chrom本地调试 selenium调试本地浏览器

chrom本地调试  selenium调试本地浏览器

1、cmd命令窗口执行下面命令,生成指定端口的chrom窗口。

 终端执行chrome.exe完整路径 --remote-debugging-port=9222 --user-data-dir="自定义用户信息文件夹路径",端口可修改。

D:\Desktop\item\chromtest\Chrome\Application>chrome.exe --remote-debugging-port=9222 --user-data-dir="D:\Desktop\item\chromtest\Chrome\Application\test"

 

2.python脚本执行

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from time import sleep
from selenium.webdriver.chrome.options import Options


chrome_options = Options()
# chrome_options.add_argument('--headless')
# chrome_options.add_argument('--disable-gpu')
# chrome_options.add_argument('--no-sandbox')
# chrome_options.add_argument('--ignore-certificate-errors')
# chrome_options.add_argument('--no-sandbox')
# chrome_options.add_argument('--disable-dev-shm-usage')
# chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])

# chrome_options.add_experimental_option('detach', True)


def check_handles():
    handles = bro.window_handles
    bro.switch_to.window(handles[-1])
def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')
    chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")

    bro = webdriver.Chrome(service=Service('./chromedriver.exe'),options=chrome_options)
    bro.get('https://baidu.com')  #此处换成自己的地址
    bro.maximize_window()
    sleep(2)
    check_handles()
    bro.find_element(By.XPATH, '//*[@id="app"]/div/div[2]/div/div[3]/div[2]/div[4]/div[2]/div[2]/div').click()  # 去答题
posted @ 2024-01-25 08:43  小小仓鼠  阅读(111)  评论(0编辑  收藏  举报