Selenium + Python:无法使用 Try & WebDriverWait 捕获 TimeoutException
Selenium + Python: Unable to catch a TimeoutException with Try & WebDriverWait
Python version: 3.10; Selenium Webdriver: Firefox; IDE: PyCharm 2021.3.2 (CE); OS: Fedora 35 VM
我正在编写一个 python selenium 脚本来从网站上抓取数据。
我想浏览一个网站,找到一个元素并打印出来。当元素存在时,我能够做到这一点。
我的问题是有时该元素不存在并且出现异常。
如果我使用此代码时元素不存在:
RemainDeductible = (WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "b8-b36-Input_RemainAmtYr1"))).get_attribute("value"))
脚本产生以下异常:
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
如果发生超时异常,我想捕获异常并将“Remain Due”变量设置为字符串“BLANK”。
如果我在我的代码中使用 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"
我仍然遇到超时异常。我以为我的代码会捕获异常。为什么抓不到???
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
我要查找的元素的相关 HTML 是:
<input id="b8-b36-Input_RemainAmtYr1"
class="form-control OSFillParent" data-
input="" disabled="" type="text"
style="margin-top: 5px;" value=".50">
event
当然,代码确实有效当元素存在时。我的问题是,当元素 not 存在时,代码因超时异常而崩溃。
当发生超时异常时,如何捕获超时异常并将“Remain Due”变量设置为字符串“BLANK”?
我会在这里提出几点建议:
- 而不是
except TimeoutException:
尝试使用
except:
我知道最好捕获特定的异常类型而不是任何异常,但我仍然建议您在此处执行此操作。
2) 我建议使用 visibility_of_element_located
而不是 presence_of_element_located
,因为 presence_of_element_located
将等待此类元素出现在页面上,而它可能仍未完全呈现。而 visibility_of_element_located
将等待更成熟的元素状态,当它可见时。
所以而不是
try:
RemainDue = (WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, "b8-b36-Input_RemainAmtYr1"))).get_attribute("value"))
except TimeoutException:
RemainDue = "BLANK"
我建议你使用
try:
RemainDue = (WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.ID, "b8-b36-Input_RemainAmtYr1"))).get_attribute("value"))
except:
RemainDue = "BLANK"
还要确保 b8-b36-Input_RemainAmtYr1
的 id
值是固定的,不会改变。
Python version: 3.10; Selenium Webdriver: Firefox; IDE: PyCharm 2021.3.2 (CE); OS: Fedora 35 VM
我正在编写一个 python selenium 脚本来从网站上抓取数据。 我想浏览一个网站,找到一个元素并打印出来。当元素存在时,我能够做到这一点。 我的问题是有时该元素不存在并且出现异常。 如果我使用此代码时元素不存在:
RemainDeductible = (WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "b8-b36-Input_RemainAmtYr1"))).get_attribute("value"))
脚本产生以下异常:
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
如果发生超时异常,我想捕获异常并将“Remain Due”变量设置为字符串“BLANK”。 如果我在我的代码中使用 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"
我仍然遇到超时异常。我以为我的代码会捕获异常。为什么抓不到???
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
我要查找的元素的相关 HTML 是:
<input id="b8-b36-Input_RemainAmtYr1"
class="form-control OSFillParent" data-
input="" disabled="" type="text"
style="margin-top: 5px;" value=".50">
event
当然,代码确实有效当元素存在时。我的问题是,当元素 not 存在时,代码因超时异常而崩溃。 当发生超时异常时,如何捕获超时异常并将“Remain Due”变量设置为字符串“BLANK”?
我会在这里提出几点建议:
- 而不是
except TimeoutException:
尝试使用
except:
我知道最好捕获特定的异常类型而不是任何异常,但我仍然建议您在此处执行此操作。
2) 我建议使用 visibility_of_element_located
而不是 presence_of_element_located
,因为 presence_of_element_located
将等待此类元素出现在页面上,而它可能仍未完全呈现。而 visibility_of_element_located
将等待更成熟的元素状态,当它可见时。
所以而不是
try:
RemainDue = (WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, "b8-b36-Input_RemainAmtYr1"))).get_attribute("value"))
except TimeoutException:
RemainDue = "BLANK"
我建议你使用
try:
RemainDue = (WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.ID, "b8-b36-Input_RemainAmtYr1"))).get_attribute("value"))
except:
RemainDue = "BLANK"
还要确保 b8-b36-Input_RemainAmtYr1
的 id
值是固定的,不会改变。