Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Selenium to manipulate a browser to open a web page

Shulou Source: shulou.com Published: 2022-06-02 01:01:04 09月23日 Update

This article will explain in detail how to use the Selenium browser to open a web page. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

01

Simply use Selenium

Let's take a look at such an example:

# coding:utf-8'''

@ author: Mr. State

@ site:zmister.com

@ Wechat official account: Mr. of the state

'' from selenium import webdriver

Driver = webdriver.Chrome (executable_path=r "D:\ chromedriver_win32\ chromedriver.exe")

Driver.get ('http://www.baidu.com')

Inputs = driver.find_element_by_id ('kw')

Inputs.clear ()

Inputs.send_keys ("Mr. State")

Print (driver.title)

By running the above code, our computer will automatically launch a Chrome browser, open the home page of Baidu, then enter "Mr. State" in the input box of Baidu, and finally enter the title of the web page on the console:

02

The first code interpretation

In the above program, we first import the webdriver submodule from selenium, and webdriver provides the implementation of all browser drivers:

From selenium import webdriver

In the current version of selenium, webdriver implements the interfaces of most browsers:

Next, we instantiate the webdriver of a Chrome browser:

Driver = webdriver.Chrome (executable_path=r "D:\ chromedriver_win32\ chromedriver.exe")

In the Chrome () method, we specify the path to the previously downloaded chromedriver.exe through executable_path.

The get () method of driver manipulates the browser to open a specified URL, which waits for the page to be fully loaded:

Driver.get ("http://www.baidu.com")"

After waiting for the page to load, we use the find_element_by_id () method to locate and find the page input box element through the element's ID.

In selenium, webdriver provides a number of methods to find elements, all of which are named find_element_by_ *:

Inputs = driver.find_element_by_id ('kw')

After navigating to the input search box on the home page of Baidu through the id value of the input box, we then use the clear () method to clear the contents of the input box (although there is no content in the input box), and then use the send_keys () method to send a string to the input box:

Inputs.clear () inputs.send_keys ("seven dyes")

Finally, we printed the title of the page:

Print (driver.title)

In this way, we use Selenium to complete a simple web page automation operation.

03

Summary

We complete the preliminary introduction of Selenium through a simple example-manipulating the Chrome browser to open the home page of Baidu and entering search terms in the input box.

On how to use Selenium to manipulate the browser to open the web page to share here, I hope the above content can be of some help to you, you can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Input browser browse method web page sir content page element home page location code article more title knowledge program article search good Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS NVidia Shulou Information Huawei OPPO Reno