【发布时间】: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