簡體   English   中英

如何修復 AttributeError: 'NoneType' 對象沒有屬性 'click'

[英]How to fix AttributeError: 'NoneType' object has no attribute 'click'

如何解決錯誤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沒有返回任何元素,因此None從沒有屬性為“click”的except塊返回。

由於您的用是單擊帶有文本的元素,因為有幾個事實:

  • 您不需要單獨等待主頁加載WebDriverWait 所以你可以刪除方法wait_for_home_page_to_load(self)
  • 相反,一旦您為 url https://huew.co/調用get()為所需元素引入WebDriverWait ,即帶有文本的元素為You to be clickable
  • 捕獲實際異常TimeoutException會更好
  • 不確定您的用例,但沒有必要返回None而是打印相關文本並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
  • 您必須添加以下導入:

     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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM