Python Selenium显示OS E

2024-09-30 01:24:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经运行了下面的代码,并期望它将在一个新的标签页中打开新的URL(google主页),只要它满足条件(必须打开新的google页面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] Only one usage of each socket address (protocol/network address/port) is normally permitted

我真的不知道港口。如果是关于端口的问题,那么您能告诉我如何在python中使用端口,以及在代码中在哪里使用它来避免这种异常吗。在

你能帮我吗?在


Tags: fromimportfalsedatetimeiftimedriverselenium
1条回答
网友
1楼 · 发布于 2024-09-30 01:24:12

我觉得在处理窗户方面有点差距。要打开一个新的选项卡/窗口并切换到该窗口,请引入一点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)

相关问题 更多 >

    热门问题