selenium WebDriver 复用浏览器

要求:

  • 需要退出当前所有的谷歌浏览器(特别注意)
  • 找到chrome的启动路径
    • 启动命令windows:chrome --remote-debugging-port=9222
    • 启动命令mac:Google\ Chrome --remote-debugging-port=9222
  • 配置环境变量   
  • mac 启动路径

    • /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
复制代码
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

class TestTestdemo():
def setup_method(self):
options
= Options()
options.debugger_address
= "127.0.0.1:9222"
self.driver
= webdriver.Chrome(options=options)
self.driver.implicitly_wait(
5)

</span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> teardown_method(self, method):
    self.driver.quit()

</span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> test_testdemo(self):
    self.driver.get(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">http://www.baidu.com</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
    sleep(</span>3)</pre>
复制代码

end



posted @ 2021-07-18 16:57  R-Bear  阅读(93)  评论(0编辑  收藏  举报