[英]Selenium TimeoutException not handling error
I'm writing a script that tests username and passwords against a list of IPs.我正在编写一个脚本,根据 IP 列表测试用户名和密码。 There are four outcomes:
有四种结果:
For some reason, the TimeoutException is not handling the error.出于某种原因,TimeoutException 没有处理错误。 The script crashes when an IP is not reachable.
无法访问 IP 时脚本会崩溃。
#!/usr/bin/env python3
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
username = "8000"
password = "0"
def default_password(site_ip):
ops = webdriver.ChromeOptions()
ops.add_argument('--ignore-certificate-errors')
ops.add_argument('--ignore-ssl-errors')
ops.add_argument('--headless')
driver = webdriver.Chrome('/mnt/c/Windows/chromedriver.exe',options=ops)
try:
driver.get("https://"+site_ip)
except TimeoutException as ex:
print("Connection timed out")
driver.close()
try:
driver.find_element(By.ID, "username").send_keys(username)
driver.find_element(By.ID, "password").send_keys(password)
driver.find_element(By.ID, "submit").click()
except NoSuchElementException as ex:
print ("No login page. Try "+site_ip+" in browser.")
driver.close()
try:
success = WebDriverWait(driver, 80).until(EC.presence_of_element_located((By.ID, "loggedInUsername")))
if 'success' in locals():
print("Login successful with default password for "+ site_ip)
driver.close()
except TimeoutException as ex:
print ("Login Unsuccessful")
driver.close()
default_password(192.168.0.1)
Here's the Traceback I get (removed identifying information):这是我得到的 Traceback(删除了识别信息):
DevTools listening on ws://127.0.0.1:59113/devtools/browser/2bc2e501-4b4a-4d9a-989b-5e72c7b25a68
Traceback (most recent call last):
File "/default_pass.py", line 56, in <module>
default_password("xx.xx.xx.xx")
File "/default_pass.py", line 24, in default_password
driver.get("https://"+site_ip)
File "/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 447, in get
self.execute(Command.GET, {'url': url})
File "/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 435, in execute
self.error_handler.check_response(response)
File "/.local/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: net::ERR_CONNECTION_TIMED_OUT
(Session info: headless chrome=103.0.5060.114)
Stacktrace:
Backtrace:
Ordinal0 [0x00616463+2188387]
Ordinal0 [0x005AE461+1762401]
Ordinal0 [0x004C3D78+802168]
Ordinal0 [0x004C04E8+787688]
Ordinal0 [0x004B654D+746829]
Ordinal0 [0x004B710A+749834]
Ordinal0 [0x004B675A+747354]
Ordinal0 [0x004B5D3F+744767]
Ordinal0 [0x004B4C28+740392]
Ordinal0 [0x004B50FD+741629]
Ordinal0 [0x004C5544+808260]
Ordinal0 [0x0051D2DD+1168093]
Ordinal0 [0x0050C7DC+1099740]
Ordinal0 [0x0051CC22+1166370]
Ordinal0 [0x0050C5F6+1099254]
Ordinal0 [0x004E6BE0+945120]
Ordinal0 [0x004E7AD6+948950]
GetHandleVerifier [0x008B71F2+2712546]
GetHandleVerifier [0x008A886D+2652765]
GetHandleVerifier [0x006A002A+520730]
GetHandleVerifier [0x0069EE06+516086]
Ordinal0 [0x005B468B+1787531]
Ordinal0 [0x005B8E88+1805960]
Ordinal0 [0x005B8F75+1806197]
Ordinal0 [0x005C1DF1+1842673]
BaseThreadInitThunk [0x75B2FA29+25]
RtlGetAppContainerNamedObjectPath [0x77157A7E+286]
RtlGetAppContainerNamedObjectPath [0x77157A4E+238]
```
You were close enough.你离得够近了。 Instead of sending the raw ip address ie
xx.xx.xx.xx
you need to pass it as a string as follows which will get appended to https://
within default_password(site_ip)
:而不是发送原始 IP 地址,即
xx.xx.xx.xx
您需要将其作为字符串传递,如下所示,该字符串将附加到https://
default_password(site_ip)
:
default_password("198.168.34.18")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.