![](/img/trans.png)
[英]How to fix AttributeError: 'NoneType' object has no attribute 'click' in insta bot
[英]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
塊返回。
由於您的用例是單擊帶有文本的元素,因為您有幾個事實:
wait_for_home_page_to_load(self)
。https://huew.co/
調用get()
為所需元素引入WebDriverWait ,即帶有文本的元素為You to be clickable 。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.