先 用命令行 开启 debug 模式的 chrome
/Applications/Google Chrome.app/Contents/MacOS/Google Chrome --remote-debugging-port=9222 --user-data-dir="/Users/paul/Desktop/seleniumProfile"
然后 py selenium代码
#! /usr/bin/env python3
# -*- coding: UTF-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time
DRIVER_PATH = "/Users/paul/code/myEnvironment/chromedriver" # /Users/paul/code/myEnvironment/chromedriver # ./driver/chromedriver.exe
class Driver:
def __init__(self):
# 初始化驱动
self.driver = None
self._init_driver()
def _init_driver(self):
if self.driver:
return
option = webdriver.ChromeOptions()
# 这一句重要:用于连接我们 debug 模式的 chrome
option.add_experimental_option('debugerAddress', '127.0.0.1:9222')
option.add_argument("--disable-blink-features")
option.add_argument("--disable-blink-features=AutomationControlled")
self.driver = webdriver.Chrome(options=option, executable_path=DRIVER_PATH) # /Users/paul/code/myEnvironment/chromedriver
def doSomething(self, content):
print("sending start")
# ...
self.driver.find_element_by_css_selector(SubmitButton).click()
print("all done")
if __name__ == '__main__':
print("in main")
work = Driver()
work.doSomething("test from slelenium #test")
print("end main")