如何解决错误AttributeError: 'NoneType' object has no attribute 'click'
?它在self.home.get_you_button().click()
失败。它的工作很好,当我没有创建页面对象类...它点击了按钮,你没有任何错误,但通过使用POM它的失败。网址是https://huew.co/
代码试验:
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
class HomePage():
def __init__(self,driver):
self.driver = driver
def wait_for_home_page_to_load(self):
wait =WebDriverWait(self.driver,30)
wait.until(expected_conditions.visibility_of(self.driver.find_element_by_tag_name('html')))
def get_you_button(self):
try:
element = self.driver.find_element_by_xpath("//div[@class='desktop-public-header']/a[@ng-controller='UserNavigationInteractionCtrl'][6]")
except:
return None
此错误消息...
AttributeError: 'NoneType' object has no attribute 'click'
...意味着没有要素被WebDriverWait返回所以都不是从没有属性为“点击”的except
块返回。
当你的用例是点击的文本,你几个事实的元素:
wait_for_home_page_to_load(self)
。get()
的URL https://huew.co/
诱导WebDriverWait为所需的元素,即元素与文字为你点击。break
self.driver = driver
try:
return (WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class= 'desktop-menu-container ng-scope' and @href='/profile/']"))))
print("YOU link found and returned")
except TimeoutException:
print("YOU link not found ... breaking out")
break