How does selenium continue to run automation scripts on open browsers?

foreword

When using selenium for web automation, we often encounter such a requirement. Can we continue to run automation scripts on the basis of already opened browsers?
In this way, the previous verification code login can be manually clicked, and the subsequent pages can be executed using scripts, which can solve a big pain point.

Start the browser from the command line

First right-click the desktop icon of the Chrome browser and find the installation path of chrome.exe

Copy the address C:\Program Files\Google\Chrome\Applicationand add it to the environment variable Path

Open cmd and enter the command to start the chrome browser

  • –remote-debugging-port is the specified running port, you can set the easy port, as long as it is not occupied
  • –user-data-dir specifies the operating data of the running browser, and creates a new clean directory without affecting the original data of the system
> chrome.exe --remote-debugging-port=9222 --user-data-dir="D:\selenium_chrome"

After execution, the chrome browser will be launched

selenium running has opened the browser

Enter my blog address on the opened browser:https://www.cnblogs.com/yoyoketang/

Create a new py file, which can be run directly on the browser without restarting the browser

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = webdriver.Chrome(chrome_options=chrome_options)
# 接着运行
print(driver.current_url)
print(driver.title)

operation result

https://www.cnblogs.com/yoyoketang/
上海-悠悠 - 博客园

This method can solve the problem of the verification code on the login page. The verification code is manually operated, and the following pages continue to be operated with code.

Guess you like

Origin blog.csdn.net/qq_27371025/article/details/119669566