首页 > 解决方案 > 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

我在末尾添加了“NoSuchElementException”,现在我收到了这个错误:

Traceback (most recent call last):
  File "<input>", line 4, in <module>
  File "C:\Users\Oli\PycharmProjects\excel_example\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "C:\Users\Oli\PycharmProjects\excel_example\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Users\Oli\PycharmProjects\excel_example\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Oli\PycharmProjects\excel_example\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"lieferschein"}
  (Session info: chrome=71.0.3578.98)
  (Driver info: chromedriver=2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 10.0.17134 x86_64)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<input>", line 8, in <module>
TypeError: 'WebElement' object is not callable

标签: pythonselenium

解决方案


由于您使用的是except: pass,这会捕获所有可能的异常,因此暂时忽略发生的一些异常,但稍后会默默地产生问题,这将更难调试。

NoSuchElementException异常由 引发.find_element_by_id('anyID'),因此最好明确提及它except

try:
    tab_check = driver.find_element_by_id('lieferschein')
    # break
except NoSuchElementException:
    print('No element of that id present!')

推荐阅读