python+selenium2自动化---复用已有的浏览器

使用步骤:

1、将Chrome浏览器安装路径添加到系统环境变量中(或者进入安装目录),执行

chrome --remote-debugging-port=9222

2、在步骤1打开的浏览器中登录要操作的网站;

3、python代码如下,这样就可以免登录,直接操作已经登录的页面了:

from time import sleep

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class TestCase():
    def __init__(self):
        options = Options()
        options.debugger_address = '127.0.0.1:9222'
        self.driver = webdriver.Chrome(chrome_options=options)

        WebDriverWait(self.driver, 60, 0.5).until(
            EC.visibility_of_element_located(('xpath','//*[@id="workzone"]/div[2]/ul[1]/li[1]/div/span[2]'))).click()

        sleep(3)

if __name__ == '__main__':
    TestCase()

注意点,好像需要先关闭其他所有的浏览器,不然没法复用。

原文地址:https://www.cnblogs.com/canghai1024/p/13490905.html