Selenium timeoutexception不处理错误

发布于 2025-02-13 15:59:24 字数 3843 浏览 1 评论 0原文

我正在编写一个针对IPS列表来测试用户名和密码的脚本。有四个结果:

  1. IP/网页无法访问。
  2. 没有登录页面。
  3. 用户名/密码失败。
  4. 登录成功。

由于某种原因,TimeOutException无法处理错误。当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)

这是我得到的追溯(已删除识别信息):

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]
    ```

I'm writing a script that tests username and passwords against a list of IPs. There are four outcomes:

  1. The IP/webpage is not reachable.
  2. There is no login page.
  3. The username/password fail.
  4. The login is successful.

For some reason, the TimeoutException is not handling the error. The script crashes when an IP is not reachable.

#!/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):

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]
    ```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷梦 2025-02-20 15:59:24

你足够近。而不是发送原始IP地址IE xx.xx.xx.xx.xx 您需要以下作为字符串将其传递,该字符串将被附加到 https:// <代码> default_password(site_ip):

default_password("198.168.34.18")

You were close enough. Instead of sending the raw ip address i.e. 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):

default_password("198.168.34.18")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文