1. 程式人生 > 其它 >selenium WebDriver 複用瀏覽器

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