如何解决AttributeError的:“NoneType”对象有没有属性“点击”

问题描述 投票:-3回答:1

如何解决错误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
python python-3.x selenium selenium-webdriver webdriverwait
1个回答
0
投票

此错误消息...

AttributeError: 'NoneType' object has no attribute 'click'

...意味着没有要素被WebDriverWait返回所以都不是从没有属性为“点击”的except块返回。

当你的用例是点击的文本,你几个事实的元素:

  • 你并不需要等待主页seperately与WebDriverWait加载。所以,你可以删除方法wait_for_home_page_to_load(self)
  • 相反,诱导一旦你调用get()的URL https://huew.co/诱导WebDriverWait为所需的元素,即元素与文字为你点击。
  • 这将是更好的捕捉到实际的异常TimeoutException异常
  • 不知道你的使用情况,但没有点返回无而打印相关的文字和qazxsw POI。
  • 您可以采用如下方案: 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
© www.soinside.com 2019 - 2024. All rights reserved.