使用步驟:
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()
注意點,好像需要先關閉其他所有的瀏覽器,不然沒法復用。