简体   繁体   English

Selenium TimeoutException 未处理错误

[英]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:有四种结果:

  1. The IP/webpage is not reachable.无法访问 IP/网页。
  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.出于某种原因,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.

相关问题 Selenium TimeoutException 处理不起作用 - Selenium TimeoutException handling doesn't work 在 selenium python 上出现 TimeoutException 错误 - getting TimeoutException error on selenium python Python Selenium TimeoutException 错误,即使等待可点击,但 TimeoutException 除外: - Python Selenium TimeoutException error even with wait until clickable and except TimeoutException: 超时异常 Selenium - TimeoutException Selenium Python selenium Webdriver:如何修复此错误“TimeoutException” - Python selenium Webdriver: How to fixe this error "TimeoutException" 错误:selenium.common.exceptions.TimeoutException:消息: - error: selenium.common.exceptions.TimeoutException: Message: 自动点击带有 Selenium 的按钮。错误引发 TimeoutException - Automatic click on button with Selenium. Error raise TimeoutException 有没有办法让 selenium 忽略 timeoutexception 错误并在发生时继续? - Is there a way to let selenium ignore the timeoutexception Error and continue if it happens? 如何使用硒单击图像链接导致TimeoutException错误 - How to click on an image link using selenium resulting in TimeoutException error Selenium DriverGet 抛出错误 java.util.concurrent.TimeoutException - Selenium DriverGet throws Error java.util.concurrent.TimeoutException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM