TimeoutException对象在Selenium中不可调用

huangapple go评论52阅读模式
英文:

TimeoutException object is not callable in Selenium

问题

在运行以下代码时,我得到了TimeoutException对象不可调用的错误。

driver = webdriver.Chrome(options=options)

try:
   WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='ui-datepicker-div']//a[@class='ui-state-default']"))).click()

except exceptions.TimeoutException as te:

   Do something

完整的错误消息如下:

open_date_found = WebDriverWait(driver, 2, ).until(ec.element_to_be_clickable((By.XPATH, "//div[@id='ui-datepicker-div']//a[@class='ui-state-default']"))).click()
TypeError: 'TimeoutException'对象不可调用
python-BaseException

错误消息出现在代码的第二行运行时,甚至没有捕捉到异常。

更新
我已将代码更改如下以忽略异常。但是,我仍然遇到了TimeoutException错误。Selenium不会忽略它!

open_date_found = WebDriverWait(driver, 2, .5, TimeoutException).until(ec.element_to_be_clickable((By.XPATH, "//div[@id='ui-datepicker-div']//a[@class='ui-state-default']"))).click()
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
Backtrace:
	(No symbol) [0x003037D3]
	(No symbol) [0x00298B81]
	(No symbol) [0x0019B36D]
	(No symbol) [0x001CD382]
	(No symbol) [0x001CD4BB]
	(No symbol) [0x00203302]
	(No symbol) [0x001EB464]
	(No symbol) [0x00201215]
	(No symbol) [0x001EB216]
	(No symbol) [0x001C0D97]
	(No symbol) [0x001C253D]
	GetHandleVerifier [0x0057ABF2+2510930]
	GetHandleVerifier [0x005A8EC1+2700065]
	GetHandleVerifier [0x005AC86C+2714828]
	GetHandleVerifier [0x003B3480+645344]
	(No symbol) [0x002A0FD2]
	(No symbol) [0x002A6C68]
	(No symbol) [0x002A6D4B]
	(No symbol) [0x002B0D6B]
	BaseThreadInitThunk [0x76777D69+25]
	RtlInitializeExceptionChain [0x7790BB9B+107]
	RtlClearBits [0x7790BB1F+191]
	(No symbol) [0x00000000]

非常感谢您的帮助。

英文:

In running the below code, I get TimeoutException object is not callable.

driver = webdriver.Chrome(options=options)

try:
   WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='ui-datepicker-div']//a[@class='ui-state-default']"))).click()

except exceptions.TimeoutException as te:

   Do something

The complete error message is below:


open_date_found = WebDriverWait(driver, 2, ).until(ec.element_to_be_clickable((By.XPATH, "//div[@id='ui-datepicker-div']//a[@class='ui-state-default']"))).click()
TypeError: 'TimeoutException' object is not callable
python-BaseException

The error message comes in running the second line of the code, it does not even catch the exception.

Update:
I changed the code as below to ignore the exception. However, I get TimeoutException error. Selenium does not ignore it!

    open_date_found = WebDriverWait(driver, 2, .5, TimeoutException).until(ec.element_to_be_clickable((By.XPATH, "//div[@id='ui-datepicker-div']//a[@class='ui-state-default']"))).click()
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
Backtrace:
	(No symbol) [0x003037D3]
	(No symbol) [0x00298B81]
	(No symbol) [0x0019B36D]
	(No symbol) [0x001CD382]
	(No symbol) [0x001CD4BB]
	(No symbol) [0x00203302]
	(No symbol) [0x001EB464]
	(No symbol) [0x00201215]
	(No symbol) [0x001EB216]
	(No symbol) [0x001C0D97]
	(No symbol) [0x001C253D]
	GetHandleVerifier [0x0057ABF2+2510930]
	GetHandleVerifier [0x005A8EC1+2700065]
	GetHandleVerifier [0x005AC86C+2714828]
	GetHandleVerifier [0x003B3480+645344]
	(No symbol) [0x002A0FD2]
	(No symbol) [0x002A6C68]
	(No symbol) [0x002A6D4B]
	(No symbol) [0x002B0D6B]
	BaseThreadInitThunk [0x76777D69+25]
	RtlInitializeExceptionChain [0x7790BB9B+107]
	RtlClearBits [0x7790BB1F+191]
	(No symbol) [0x00000000]

Your help would be much appreciated.

答案1

得分: 0

不要捕获原始异常因为你已经使用了 _WebDriverWait_只捕获 _`TimeoutException`_

```python
from selenium.common.exceptions import TimeoutException

driver = webdriver.Chrome(options=options)

try:
    WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='ui-datepicker-div']//a[@class='ui-state-default']"))).click()

except TimeoutException:
    # 做一些处理
英文:

Don't catch the raw expection. As you have used WebDriverWait catch the TimeoutException only:

from selenium.common.exceptions import TimeoutException

driver = webdriver.Chrome(options=options)

try:
	WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='ui-datepicker-div']//a[@class='ui-state-default']"))).click()
	
except TimeoutException:
	# Do something

答案2

得分: 0

以下是已翻译的内容:

"The piece of code that you have shared seems to be fine, since the line can possibly throw an exception and you've handled it.
From the error itself, it seems that you are trying to do something equivalent to te() and it's that line that's causing the conflict. Can you share the error handling code?
Edit:
Try:

e = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='ui-datepicker-div']//a[@class='ui-state-default']")))
e.click()
英文:

The piece of code that you have shared seems to be fine, since the line can possibly throw an exception and you've handled it.
From the error itself, it seems that you are trying to do something equivalent to te() and it's that line that's causing the conflict. Can you share the error handling code?
Edit:
Try:

e = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='ui-datepicker-div']//a[@class='ui-state-default']")))
e.click()

huangapple
  • 本文由 发表于 2023年2月8日 21:53:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定