Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report