[英]How to fix AttributeError: 'NoneType' object has no attribute 'click'
How to address the error AttributeError: 'NoneType' object has no attribute 'click'
?如何解决错误AttributeError: 'NoneType' object has no attribute 'click'
? Its failing in self.home.get_you_button().click()
.它在self.home.get_you_button().click()
失败。 It's working fine when I am not creating Page Object Class...it clicks on the You button without any error but by using POM it's failing.当我不创建页面对象类时它工作正常......它点击你按钮没有任何错误但是通过使用 POM 它失败了。 The url is https://huew.co/网址是https://huew.co/
Code trials:代码试验:
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
This error message...这个错误信息...
AttributeError: 'NoneType' object has no attribute 'click'
...implies that no element was returned by WebDriverWait so None was returned from the except
block which have no attribute as 'click'. ...暗示WebDriverWait没有返回任何元素,因此None从没有属性为“click”的except
块返回。
As your usecase is to click on the element with text as You a couple of facts:由于您的用例是单击带有文本的元素,因为您有几个事实:
wait_for_home_page_to_load(self)
.所以你可以删除方法wait_for_home_page_to_load(self)
。get()
for the url https://huew.co/
induce WebDriverWait for the desired element ie element with text as You to be clickable .相反,一旦您为 url https://huew.co/
调用get()
为所需元素引入WebDriverWait ,即带有文本的元素为You to be clickable 。break
.不确定您的用例,但没有必要返回None而是打印相关文本并break
。You can use the following solution:您可以使用以下解决方案:
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
You have to add the following imports :您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.