先 用命令行 开启 debug 模式的 chrome
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222 --user-data-dir="/Users/paul/Desktop/seleniumProfile"
然后 py selenium代码
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"
class Driver:
def __init__(self):
self.driver = None
self._init_driver()
def _init_driver(self):
if self.driver:
return
option = webdriver.ChromeOptions()
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)
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")
|