Chromium自动化 由于Chrome版本更新太频繁,每次都需要手动更新chromedriver太麻烦,于是尝试使用固定版本的 Chromium。
Chromium和Chromedriver下载 https:// download-chromium.appspot.com/ https:// commondatastorage.googleapis.com/chromium-browser-snapshots/i ndex.html?prefix=Win_x64/1209965/
Chromium启动 rem setx GOOGLE_API_KEY "no" rem setx GOOGLE_DEFAULT_CLIENT_ID "no" rem setx GOOGLE_DEFAULT_CLIENT_SECRET "no" chrome-win\chrome.exe --remote-debugging-port=9990 --user-data-dir=D:\Software\chromium\user_data_dir --no-default-browser-check
selenium示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 from selenium import webdriverfrom selenium.webdriver.common.by import By driver = webdriver.Chrome() driver.get("https://www.selenium.dev/selenium/web/web-form.html" ) title = driver.title driver.implicitly_wait(0.5 ) text_box = driver.find_element(by=By.NAME, value="my-text" ) submit_button = driver.find_element(by=By.CSS_SELECTOR, value="button" ) text_box.send_keys("Selenium" ) submit_button.click() message = driver.find_element(by=By.ID, value="message" ) text = message.text driver.quit()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 from selenium.webdriver.common.action_chains import ActionChainsfrom selenium import webdriverfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.support.wait import WebDriverWaitimport timefrom loguru import loggerdef exchange (driver, code, username ): while True : driver.get('https://www.eledui.com:8887/codeExchange' ) try : WebDriverWait(driver, 30 ).until(EC.presence_of_element_located((By.XPATH, '//input[@placeholder="请输入权益码"]' ))) print ('find input success' ) except : print ('not found input' ) continue code_input =driver.find_element(by=By.XPATH, value='//input[@placeholder="请输入权益码"]' ) code_input.send_keys(code) submit_button = driver.find_element(by=By.XPATH, value='//button' ) submit_button.click() try : WebDriverWait(driver, 30 ).until(EC.presence_of_element_located((By.CLASS_NAME, 'giftBox-item' ))) print ('find giftBox-item success' ) except : print ('not found giftBox-item' ) continue giftBox1 = driver.find_element(by=By.XPATH, value='//div[@class="giftBox1"]' ) giftBox1.click() time.sleep(0.3 ) submit_button = driver.find_element(by=By.XPATH, value='//button' ) submit_button.click() try : WebDriverWait(driver, 30 ).until(EC.presence_of_element_located((By.XPATH, '//input[@placeholder="此处必须为淘宝账号"]' ))) print ('find taobao-input success' ) except : print ('not found taobao-input' ) continue username_input =driver.find_element(by=By.XPATH, value='//input[@placeholder="此处必须为淘宝账号"]' ) username_input.send_keys(username) try : WebDriverWait(driver, 999 ).until(EC.presence_of_element_located((By.XPATH, '//div[@class="tipsSuccess"]' ))) print ('find success flag' ) db.set (code, '1' ) break except : print ('not found flag' ) continue return def main (): binary_location = r'D:\Software\chromium\chrome-win\chrome.exe' executable_path = r'D:\Software\chromium\chrome-win\chromedriver.exe' logger.add('log.txt' ) options = webdriver.ChromeOptions() options.binary_location = binary_location options.add_experimental_option("debuggerAddress" , "127.0.0.1:9990" ) service = Service(executable_path=executable_path) driver = webdriver.Chrome(options=options, service=service) username = 'username' codes = ['1' , '2' , '3' ] for code in codes: code = code.strip() logger.info(f'{code} {username} ' ) exchange(driver, code, username)if __name__ == '__main__' : main()
Playwright示例 pip install playwright python -m playwright install Chromium
from playwright.sync_api import sync_playwrightwith sync_playwright() as p: browser = p.chromium.launch() page = browser.new_page() page.goto("https://www.baidu.com" ) print (page.title()) browser.close()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 from playwright.sync_api import Playwright, sync_playwright, expectdef run (playwright: Playwright ) -> None : code = '111111' username = 'username' browser = playwright.chromium.connect_over_cdp("http://localhost:9990" ) default_context = browser.contexts[0 ] page = default_context.pages[0 ] page.goto('https://www.eledui.com:8887/codeExchange' ) page.get_by_placeholder("请输入权益码" ).fill(code) page.locator('xpath=//button' ).click() page.locator('xpath=//div[@class="giftBox1"]' ).click() page.locator('xpath=//button' ).click() page.locator('xpath=//input[@placeholder="此处必须为淘宝账号"]' ).fill(username) page.locator('xpath=//button[@class="rbtn"]' ).click() with sync_playwright() as playwright: run(playwright)
参考链接 https://playwright.dev/python/docs/library
https://www.selenium.dev/documentation/webdriver/getting_started/first_script/
https://googlechromelabs.github.io/chrome-for-testing/
https://chromedevtools.github.io/devtools-protocol/tot/
https://registry.npmmirror.com/binary.html?path=chromedriver/
https://chromium.woolyss.com/download/