python - 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
解决方案
由于您使用的是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!')
推荐阅读
- python - 从字典列表中选择字典并进行一些更新
- c# - 如何在 sitefinity 中更新 NewsItem.LastModified?
- python - 我在 python 中有一个有趣的问题我无法理解它是关于返回值和富有成效的函数我猜
- r - 分组后如何统计具有一定条件的数据
- android - Android Studio 在更新到 3.5.2 后表现异常
- r - 使用 ECharts4R 在单个图表上创建多个网格
- php - 如何在 PHP 操作中包含多个产品 ID
- c# - Dockerized 集成测试 ASP.NET Core 应用程序与 RabbitMQ 的连接
- python-3.x - 代码对所有项目保持相同的运行
- java - 当有多个进程并行运行时,只消耗表中的一条记录进行处理