Use selenium to get web page source code

After installing selenium and downloading WebDriver, you can test it. The following demonstrates how to open a URL and obtain the source code of the website.

The basic steps are as follows:

1. Import webdriver

from seleniumimport webdriver

2. Create a browser action object

Specify the downloaded webdriver file path, I have copied the file to the path where the current program is running, so specify the file name directly, otherwise add the path.

path =“chromedriver.exe”

browser = webdriver.Chrome(path)

3. Visit a specified website, I use Baidu as a test instance

url =“https://www.baidu.com”

browser.get(url)

4. Test to get the source code of the webpage

content = browser.page_source

print(content)

The source code of the webpage can be completely obtained:

Guess you like

Origin blog.csdn.net/flysh05/article/details/122768946