Selenium find_element 尝试除了“WebElement”对象不可调用

我在网页上搜索 ID lieferschein。


如果我在没有 try 和 except 块的情况下进行搜索,我可能会找到 ID,


tab_check = driver.find_element_by_id('lieferschein')

如果不


try:

    tab_check = driver.find_element_by_id('lieferschein')

    # break

except:

    pass

我收到这样的错误:


Traceback (most recent call last):

  File "<input>", line 3, in <module>

TypeError: 'WebElement' object is not callable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "<input>", line 6, in <module>

TypeError: 'WebElement' object is not callable

全码:


from selenium import webdriver

import pickle


from selenium.common.exceptions import NoSuchElementException


abURL = 'https://farm01.afterbuy.de/afterbuy/auktionsliste.aspx?AWebayname=&AWFilter=37&AWSuchwort=&AWRENummer=&AWFilter2=0&awmaxart=500&maxgesamt=1000&AWEmail=&AWDatumVon=&AWDatumBis=&AWBezug=EndeDerAuktion&AWPLZ=&AWBetrag=&AWBetragBezug=1&AWStammID=&AWLaenderkennung=&AWLaenderkennungBezug=rechnung&AWLabelDynSearchField1=ShippingAddress&AWDynSearchField1=&AWLabelDynSearchField2=AlterItemNumber1&AWDynSearchField2=&AWDynamicSorting=0&AWLabelDynSearchField3=AlterItemNumber&AWDynSearchField3=&searchUserTag1=0&searchUserTag2=0&searchUserTag3=0&searchUserTag4=0&killordersession=0&art=SetAuswahl'


download_dir = "C:\\Users\\Oli\\Documents"

options = webdriver.ChromeOptions()


driver = webdriver.Chrome()


driver.get(abURL)


cookies = pickle.load(open("cookies.pkl", "rb"))

for cookie in cookies:

    driver.add_cookie(cookie)


for tab in driver.window_handles:

    driver.switch_to.window(tab)

    try:

        tab_check = driver.find_element_by_id('lieferschein')

        # break

    except NoSuchElementException:

        pass



ibeautiful
浏览 240回答 1
1回答

守候你守候我

由于您正在使用except: pass,这会捕获所有可能的异常,一些发生的异常暂时被忽略,但稍后会默默地制造一个更难调试的问题。NoSuchElementException异常由 引发.find_element_by_id('anyID'),因此最好为 明确提及它except。try:&nbsp; &nbsp; tab_check = driver.find_element_by_id('lieferschein')&nbsp; &nbsp; # breakexcept NoSuchElementException:&nbsp; &nbsp; print('No element of that id present!')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python