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

时间:2019-02-07 19:02:36

标签: python selenium

我正在一个网页上搜索ID lieferschein

如果我不加尝试地进行搜索,除了阻止,我有可能找到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

1 个答案:

答案 0 :(得分:1)

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