简体   繁体   English

Selenium + Python:无法使用 Try & WebDriverWait 捕获 TimeoutException

[英]Selenium + Python: Unable to catch a TimeoutException with Try & WebDriverWait

Python version: 3.10; Python 版本:3.10; Selenium Webdriver: Firefox; Selenium 网络驱动程序:Firefox; IDE: PyCharm 2021.3.2 (CE); IDE: PyCharm 2021.3.2 (CE); OS: Fedora 35 VM操作系统:Fedora 35 虚拟机

I am writing a python selenium script to scrape data from a website.我正在编写一个 python selenium 脚本来从网站上抓取数据。 I would like to navigate through a website, find an element & print it.我想浏览一个网站,找到一个元素并打印出来。 I am able to do this when the element is present.当元素存在时,我能够做到这一点。 My problem is that sometimes the element is not present & I get an exception.我的问题是有时该元素不存在并且出现异常。 If the element is not present when I use this code:如果我使用此代码时该元素不存在:

 RemainDeductible = (WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "b8-b36-Input_RemainAmtYr1"))).get_attribute("value"))

the script produces the following exception:该脚本产生以下异常:

 Traceback (most recent call last):
  
 File "...PythonSeleniumScript.py", line 152, in <module>
    RemainDue = (WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "b8-b36-Input_RemainAmtYr1"))).get_attribute("value"))

 File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/support/wait.py", line 89, in until
    raise TimeoutException(message, screen, stacktrace)

 selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:183:5
NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.jsm:395:5
element.find/</<@chrome://remote/content/marionette/element.js:300:16

If the Timeout Exception occurs I would like to catch the exception & set the "Remain Due" variable to the string "BLANK".如果发生超时异常,我想捕获异常并将“Remain Due”变量设置为字符串“BLANK”。 I still get the following Timeout Exception if I use Try in my code as follows:如果我在我的代码中使用 Try ,我仍然会得到以下超时异常:

try:
    RemainDue = (WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, "b8-b36-Input_RemainAmtYr1"))).get_attribute("value"))
except TimeoutException:
    RemainDue = "BLANK"

I still get the Timeout Exception.我仍然得到超时异常。 I thought my code would catch the exception.我以为我的代码会捕获异常。 Why is it not catching it???为什么抓不到???

 Traceback (most recent call last):
  
 File "...PythonSeleniumScript.py", line 155, in <module>
    RemainDue = (WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, "b8-b36-Input_RemainAmtYr1"))).get_attribute("value"))

  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/support/wait.py", line 89, in until

    raise TimeoutException(message, screen, stacktrace)

 selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:183:5
NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.jsm:395:5
element.find/</<@chrome://remote/content/marionette/element.js:300:16

The relevant HTML for the element I am trying to locate is:我要查找的元素的相关 HTML 是:

<input id="b8-b36-Input_RemainAmtYr1"
class="form-control OSFillParent" data-
input="" disabled="" type="text"
style="margin-top: 5px;" value="$10.50">
event

Of course the code does work when the element is present .当然,当元素存在时,代码确实有效。 My issue is that the code crashes with the Timeout Exception when the element is not present.我的问题是,当元素存在时,代码因超时异常而崩溃。 When the Timeout Exception occurs how do I catch the Timeout Exception & set the "Remain Due" variable to the string "BLANK"?当发生超时异常时,如何捕获超时异常并将“Remain Due”变量设置为字符串“BLANK”?

I would suggest several things here:我会在这里建议几件事:

  1. instead of代替
except TimeoutException:

Try using尝试使用

except:

I understand that it's preferable to catch a specific exception type rather any exception, but I will still advice doing that here.我知道最好捕获特定的异常类型而不是任何异常,但我仍然建议在这里这样做。
2) I would suggest using visibility_of_element_located instead of presence_of_element_located since presence_of_element_located will wait for presence of such element on the page only while it may still not be completely rendered. 2) 我建议使用visibility_of_element_located而不是presence_of_element_located因为presence_of_element_located只会等待页面上存在此类元素,而它可能仍未完全呈现。 While visibility_of_element_located will wait for much more mature element state, when it is visible.visibility_of_element_located将等待更成熟的元素 state,当它可见时。
So instead of所以而不是

try:
    RemainDue = (WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, "b8-b36-Input_RemainAmtYr1"))).get_attribute("value"))
except TimeoutException:
    RemainDue = "BLANK"

I would suggest you using我建议你使用

try:
    RemainDue = (WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.ID, "b8-b36-Input_RemainAmtYr1"))).get_attribute("value"))
except:
    RemainDue = "BLANK"

Also make sure the id value of b8-b36-Input_RemainAmtYr1 is fixed, not changing.还要确保b8-b36-Input_RemainAmtYr1id值是固定的,不会改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM