본질적으로 동일한 코드 행이 한 함수에서 작동하지만 다른 코드에서는 작동하지 않는 이유를 파악하려고합니다.find_element_by_xpath는 한 함수에서 정상적으로 작동하지만 다음 함수에서는 작동하지 않습니다. - nonetype 오류
아래와 같이 세 개의 파일이 있습니다.
내 문제는 default_login()의 벌금을 실행하고 find_element_by_xpath 라인에는 문제가 없지만 다음 오류로 find_element_by_xpath 라인 밖으로 (LOG_OUT 실행되지 않습니다) 폭탄이다 -
AttributeError: 'NoneType' object has no attribute 'find_element_by_xpath'
함수가 정의되지 않은 파일에서이 동일한 행을 독립적으로 실행할 수 있으며 문제없이 요소를 찾습니다.
가의 tearDown() 라인 self.driver.quit()를 실행할 때 나는 또한 동일 (유사) 오류가: -
AttributeError: 'NoneType' object has no attribute 'quit'
가 나는 것 같이 실패 이유를 아무도 말해 줄 수 있다면 크게 감사드립니다. 나는이 같은 문제의 모든 종류가 있어요 그리고 ... 내가 뭔가 잘못 어딘가 구성 여부를 궁금해
프로젝트 파일을 시작하고있다 : -
from selenium import webdriver
def start_webdriver():
driver = webdriver.Chrome('C:\TEMP\chromedriver.exe')
driver.maximize_window()
return driver
def start_browser(driver):
driver.get("https://myURL")
return driver
login.py initialisation.py
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
def default_login(driver):
wait = WebDriverWait(driver, 5)
login_link = driver.find_element_by_xpath("//a[contains(@href, 'login')]")
login_link.click()
username_text_field = wait.until(ec.element_to_be_clickable((By.NAME, "email")))
password_text_field = driver.find_element_by_name("password")
username_text_field.send_keys("username")
password_text_field.send_keys("password")
login_button = driver.find_element_by_xpath("//*[@class='btn save btn-primary']")
login_button.click()
def log_out(driver):
new_project = driver.find_element_by_xpath("//table[@id='my-projects-table']/tbody/tr/td[3]")
new_project.click()
logout_user = driver.find_element_by_class_name("dropdown-toggle")
logout_user.click()
logout_sign_out = driver.find_element_by_xpath("//a[contains(@href, 'logout')]")
logout_sign_out.click()
print("logged out")
just_a_test.py
import unittest
from Common import initialisation, login
class RegisterPatientsTests(unittest.TestCase):
def setUp(self):
self.driver = initialisation.start_webdriver()
self.driver = initialisation.start_browser(self.driver)
def tearDown(self):
self.driver.quit()
def test_another_quick_test(self):
self.driver = login.default_login(self.driver)
self.driver = login.log_out(self.driver)
,
감사합니다. Hoefling, 이것이 내가 필요한 것입니다! :) – Northers