简体   繁体   English

如何修复 AttributeError: 'NoneType' 对象没有属性 'click'

[英]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:由于您的用是单击带有文本的元素,因为有几个事实:

  • You don't need to wait for home page to load with WebDriverWait seperately.您不需要单独等待主页加载WebDriverWait So you can remove the method wait_for_home_page_to_load(self) .所以你可以删除方法wait_for_home_page_to_load(self)
  • Instead induce once you invoke 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
  • It will be better to catch the actual exception TimeoutException捕获实际异常TimeoutException会更好
  • Not sure about your usecase but there is no point returning None rather print the relevant text and 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.

相关问题 如何修复 AttributeError: 'NoneType' object 在 insta bot 中没有属性 'click' - How to fix AttributeError: 'NoneType' object has no attribute 'click' in insta bot “如何解决&#39;AttributeError:&#39;NoneType&#39;对象在Python中没有属性&#39;tbody&#39;错误? - "How to fix 'AttributeError: 'NoneType' object has no attribute 'tbody'' error in Python? 如何修复 AttributeError: 'NoneType' object has no attribute 'strip' in this specific code? - how to fix AttributeError: 'NoneType' object has no attribute 'strip' in this specific code? 我该如何解决这个 AttributeError: 'NoneType' object has no attribute 'text'? - How do i fix this AttributeError: 'NoneType' object has no attribute 'text'? 我该如何修复,AttributeError: 'NoneType' 对象没有属性 'send' - How can I fix, AttributeError: 'NoneType' object has no attribute 'send' 如何修复AttributeError:&#39;NoneType&#39;对象没有属性&#39;theme_cls&#39; - How to fix AttributeError: 'NoneType' object has no attribute 'theme_cls' 如何修复“”AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;app&#39; - How to fix ""AttributeError: 'NoneType' object has no attribute 'app' 如何修复:AttributeError: 'NoneType' object 没有属性 'group' - How to fix: AttributeError: 'NoneType' object has no attribute 'group' 如何解决“ AttributeError:&#39;NoneType&#39;对象没有属性&#39;text&#39;”的问题 - How to fix “AttributeError: 'NoneType' object has no attribute 'text'” for <span> 如何修复&#39;AttributeError:&#39;NoneType&#39;对象在python中没有属性&#39;get&#39;错误 - How to fix 'AttributeError: 'NoneType' object has no attribute 'get' error in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM