Python 使用 Selenium 定位 if else 条件下的 404 错误
Python Using Selenium to Locate 404 Error for If Else Condition
条件语句 - Try, Except, Else -
我正在使用 Selenium/Python 自动执行一些文件下载和上传任务。
有没有办法让 Selenium/Python 识别页面是否作为 404 加载?即我下面的 "Try" 尝试。
我的目标是让脚本在加载为 404 错误时关闭选项卡并循环直到找到可下载文件。该文件是 PDF。
我尝试使用 XPath 搜索特定文本,例如:
'Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.' - 但不确定所需的正确编码。我是新手,所以感谢您的耐心等待。
下面是脚本的底部,它也在循环中 -('icon' 是下载按钮,显示在可下载页面上,但不显示在 404 页面上)
try:
Download = driver.find_element_by_id('icon')
if Download == driver.find_element_by_id('icon'):
raise Exception
except Exception:
pyautogui.time.sleep(2)
pyautogui.hotkey('ctrl','s')
pyautogui.time.sleep(2)
pyautogui.hotkey('enter')
pyautogui.time.sleep(2)
pyautogui.hotkey('ctrl','w')
pyautogui.click(771, 374, duration=0.55)
pyautogui.click(867, 276, duration=0.55)
else:
pyautogui.time.sleep(2)
pyautogui.hotkey('ctrl','w')
pyautogui.time.sleep(2)
pyautogui.doubleClick(79, 136, duration=0.55)
pyautogui.time.sleep(15)
pyautogui.click(642, 442, duration=0.55)
pyautogui.time.sleep(2)
pyautogui.hotkey('ctrl','s')
pyautogui.time.sleep(2)
pyautogui.hotkey('enter')
pyautogui.time.sleep(2)
pyautogui.hotkey('ctrl','w')
pyautogui.click(771, 374, duration=0.55)
pyautogui.click(867, 276, duration=0.55)
上面的代码在加载时不会关闭 404 网页。相反,脚本保存网页 404 而不是尝试脚本的 "Else" 部分。
感谢您的帮助! (如果我能投赞成票,我会的。)
Webdriver 不会捕捉到 http404,这在 API 中没有实现,也不会,检查 their issue tracker. You could use just python for that, maybe this will help。
条件语句 - Try, Except, Else -
我正在使用 Selenium/Python 自动执行一些文件下载和上传任务。
有没有办法让 Selenium/Python 识别页面是否作为 404 加载?即我下面的 "Try" 尝试。
我的目标是让脚本在加载为 404 错误时关闭选项卡并循环直到找到可下载文件。该文件是 PDF。
我尝试使用 XPath 搜索特定文本,例如:
'Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.' - 但不确定所需的正确编码。我是新手,所以感谢您的耐心等待。
下面是脚本的底部,它也在循环中 -('icon' 是下载按钮,显示在可下载页面上,但不显示在 404 页面上)
try:
Download = driver.find_element_by_id('icon')
if Download == driver.find_element_by_id('icon'):
raise Exception
except Exception:
pyautogui.time.sleep(2)
pyautogui.hotkey('ctrl','s')
pyautogui.time.sleep(2)
pyautogui.hotkey('enter')
pyautogui.time.sleep(2)
pyautogui.hotkey('ctrl','w')
pyautogui.click(771, 374, duration=0.55)
pyautogui.click(867, 276, duration=0.55)
else:
pyautogui.time.sleep(2)
pyautogui.hotkey('ctrl','w')
pyautogui.time.sleep(2)
pyautogui.doubleClick(79, 136, duration=0.55)
pyautogui.time.sleep(15)
pyautogui.click(642, 442, duration=0.55)
pyautogui.time.sleep(2)
pyautogui.hotkey('ctrl','s')
pyautogui.time.sleep(2)
pyautogui.hotkey('enter')
pyautogui.time.sleep(2)
pyautogui.hotkey('ctrl','w')
pyautogui.click(771, 374, duration=0.55)
pyautogui.click(867, 276, duration=0.55)
上面的代码在加载时不会关闭 404 网页。相反,脚本保存网页 404 而不是尝试脚本的 "Else" 部分。
感谢您的帮助! (如果我能投赞成票,我会的。)
Webdriver 不会捕捉到 http404,这在 API 中没有实现,也不会,检查 their issue tracker. You could use just python for that, maybe this will help。