Selenium find\u element try除非“WebElement”对象不是callab

2024-03-28 09:44:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在网页上搜索ID lieferschein。在

如果我在没有尝试和排除块的情况下搜索,我可能会找到ID

tab_check = driver.find_element_by_id('lieferschein')

如果没有

^{pr2}$

我遇到了这样一个错误:

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

Tags: inidcheckdriverseleniumlineexceptionelement
1条回答
网友
1楼 · 发布于 2024-03-28 09:44:47

由于您使用的是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!')

相关问题 更多 >