【问题标题】:Selenium chrome Tabs are closing automaticallySelenium chrome 选项卡会自动关闭
【发布时间】:2021-01-21 05:08:31
【问题描述】:

我面临一个非常奇怪的问题,即我在一个循环中调用一个函数,该函数会打开 Selenium Chrome 选项卡。如果关闭以前的选项卡,则打开近 4 个选项卡后。 我已多次检查我的代码,但找不到任何错误或原因。

def get_chromedriver(use_proxy=False, user_agent=None):
    proxy = get_random_proxy()
    # print(proxy)
    manifest_json, background_json = get_proxy_variables(proxy[0], proxy[1], proxy[2], proxy[3])
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--start-maximized")
    chrome_options.add_argument("--log-level=3")
    chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
    if use_proxy:
        pluginfile = 'proxy_auth_plugin.zip'
        with zipfile.ZipFile(pluginfile, 'w') as zp:
            zp.writestr("manifest.json", manifest_json)
            zp.writestr("background.js", background_json)
        chrome_options.add_extension(pluginfile)
    if user_agent:
        chrome_options.add_argument('--user-agent=%s' % user_agent)
    driver = webdriver.Chrome(options=chrome_options)
    driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        "source": """
        Object.defineProperty(navigator, 'webdriver', {
          get: () => undefined
        })
      """
    })
    return driver


def open_nike_url(url, user_name, pwd):
    count = 0
    driver = get_chromedriver(True)
    driver.get(url)
    login_btn = WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CLASS_NAME, 'join-log-in')))
    time.sleep(5)
    login_btn.click()
    driver = log_in(driver, user_name, pwd)
    time.sleep(5)
    pop_up = driver.find_elements_by_class_name("nike-unite-error-close")
    # print("Outside Loop:", len(pop_up))
    return driver

for i in range(len(accounts_info)):
    open_nike_url(url, accounts_info[i][0], accounts_info[i][1])
    time.sleep(1)

代码只是尝试为每个帐户登录一个网站。 因此,在登录某些帐户后,它会关闭以前的选项卡。

【问题讨论】:

    标签: selenium selenium-webdriver selenium-chromedriver selenium-ide


    【解决方案1】:

    我认为选项卡已关闭,因为驱动程序完成了其中的任务。正如我从您的代码中了解到的那样,会为您打开的每个选项卡创建一个新的 WebDriver。这意味着当驱动程序结束其任务时,它会关闭选项卡。我认为发生这种情况是因为您正在制作所有本地上下文驱动程序。 尝试在每个司机的任务结束时为他们添加一个无限循环,看看标签是否关闭

    【讨论】:

    • 我只是覆盖了驱动程序句柄。我不明白这是什么原因,但我通过维护一个列表并为每个选项卡附加驱动程序解决了这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 2016-10-07
    相关资源
    最近更新 更多