【问题标题】:Python Selenium shows OS ErrorPython Selenium 显示操作系统错误
【发布时间】:2017-09-20 15:01:58
【问题描述】:

我已经运行了以下代码,并期望它在满足条件时会在新选项卡中打开新 URL(谷歌主页)(必须打开新的谷歌页面 URL),但它运行良好一段时间然后它抛出一个例外。

这是我的代码。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import socket
from datetime import datetime

try:


    options =webdriver.ChromeOptions()
    options.add_argument('--ignore-certificate-errors')
    options.add_argument('--ignore-ssl-errors')
    driver = webdriver.Chrome(chrome_options=options, executable_path ="C:/Users/gsssaaaa/Desktop/Python/exe.win-amd643.6/selenium/chromedriver.exe",port=80)

    driver.get('file:///C:/Users/gsssaaaa/AppData/Local/Temp/Temp1_site.zip/site/index.html')

    time.sleep(30)

    ticketopened = False
    while True:

        if driver.find_element_by_class_name('custom-select').text == "Ready":
            time.sleep(0.5)

            if driver.find_element_by_class_name('custom-select').text == "Talking":
                if ticketopened == False:

                    window = 0
                    driver.execute_script("$(window.open('https://www.google.com'))")
                    driver.switch_to_window(driver.window_handles[window])
                    window = window + 1

                    ticketopened = True
                    continue
                else:
                    ticketopened = False
            else:
                continue
        else:
            continue

except Exception as e:
    print('Exception Occured: ',e)
    print('Time and Date: '+str(datetime.now())[0:19])

这是我的错误信息:

OSError: [WinError 10048] 每个套接字地址只能使用一次 (协议/网络地址/端口)通常是允许的

我不太了解端口。如果是关于端口问题,请告诉我如何在 python 中使用端口以及在我的代码中在何处使用它以避免此异常。

你能帮我解决这个问题吗?

【问题讨论】:

  • 尝试使用Firefox webdriver。这可能会解决你的问题。我记得在某处读到 Chrome 只允许使用 1 个端口。这就是它的样子。
  • 您好,就我而言,我使用的是特定链接,除了 Chrome 之外,我无法在任何其他浏览器中打开该链接。有没有其他方法可以解决。
  • 在这里找到类似的案例:Webdriver opens thousands of connections
  • 在线程中:For anyone who is running into the same issue on Windows, I was able to "solve" it temporarily by setting TcpTimedWaitDelay value in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\ to 30 as decimal. Basically it terminates closed TCP connection after 30 seconds instead of 4 minutes (which is the default on Windows).
  • 就是这样。我没有时间深入研究它,但对此的概述似乎与Chromedriver 相关问题。如果可能,请尝试在使用后关闭 Sockets。或者如果可能的话就换其他司机。

标签: python selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

我认为在处理窗户方面存在一些差距。要打开一个新选项卡/窗口并切换到它,请输入一些WebDriverWait,然后使用索引切换到新窗口。这是打开https://www.google.co.in的示例代码块,打印Page Title,单击Gmail链接并在新选项卡中打开它,switch_to新选项卡/窗口,最后打印Page Title

driver.get('https://www.google.co.in')
print("Page Title is : %s" %driver.title)
link = driver.find_element_by_link_text('Gmail')
driver.execute_script('arguments[0].target="_blank";', link)
link.click()
wait = WebDriverWait(driver, 5)
wait.until(EC.number_of_windows_to_be(2))
driver.switch_to.window(driver.window_handles[-1])
wait.until(EC.title_contains("Gmail"))
print("Page Title is : %s" %driver.title)

【讨论】:

    猜你喜欢
    • 2014-12-22
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2020-07-13
    • 2021-01-10
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    相关资源
    最近更新 更多