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 does python operate the browser

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Most people do not understand the knowledge of this article "python how to operate the browser", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "python how to operate the browser" article.

1. Open the specified web address

When we use selenium for automated testing, after opening the browser, the first step is to let the browser access the address we specify, which can be implemented using the get method

From selenium import webdriverdriver = webdriver.Edge () driver.get ('https://www.baidu.com/') # this line is used to access the specified address 2 and get the current page url

In the course of testing, we sometimes need to obtain the url of the current page to determine whether to jump to the specified page. The method to obtain the page url is as follows:

From selenium import webdriverdriver = webdriver.Edge () driver.get ('https://www.baidu.com/')url = driver.current_url # this line is used to get the url of the current page, that is, Baidu homepage address print (url) 3, return button

The return button, that is, the ← key in the upper left corner of the browser, is simulated by clicking this button as follows

Driver.back ()

For example, enter selenium in the Baidu search box and click search, and then click the back button to achieve the following

From selenium import webdriverfrom selenium.webdriver.common.by import Byimport timedriver = webdriver.Edge () driver.get ('https://www.baidu.com/')driver.find_element(By.ID,' kw'). Send_keys ('selenium') # search box enter seleniumdriver.find_element (By.ID,' su'). Click () # Click Baidu time.sleep (3) driver.back () # to return

This code indicates that after entering selenium in the input box, click Baidu to search, and then return to Baidu home page 3 seconds later.

4. Forward button

The forward button, relative to the backward ←, is the → button in the upper left corner of the browser. Operate this button as follows:

Driver.forward ()

For example, enter selenium in the Baidu search box and click search, then click the back button and then click the forward button to achieve the following

From selenium import webdriverfrom selenium.webdriver.common.by import Byimport timedriver = webdriver.Edge () driver.get ('https://www.baidu.com/')driver.find_element(By.ID,' kw'). Send_keys ('selenium') # search box enter seleniumdriver.find_element (By.ID,' su'). Click () # Click Baidu driver.back () # return time.sleep (3) driver.forward () # forward

This code indicates that after entering selenium in the input box, click Baidu to search, then return to the operation, proceed to the forward operation after 3 seconds, and finally stay in the search results page after entering selenium.

5. Refresh the page

During the test, refreshing the page is a frequently used operation, and the selenium refresh operation method is as follows

Driver.refresh ()

Using this method is similar to pressing F5 or clicking the refresh button in the upper left corner

From selenium import webdriverdriver = webdriver.Edge () driver.get ('https://www.baidu.com/')driver.refresh() # refresh the page

This code means to open the home page of Baidu and refresh the page.

6. Get the current page title

During the test, you can use selenium to get the title of the current page as follows:

Driver.title

Use selenium to obtain the title of Baidu home page. The example is as follows:

From selenium import webdriverdriver = webdriver.Edge () driver.get ('https://www.baidu.com/')title = driver.title # get the current page titleprint (title)

After the above code is run, you will output the title of Baidu home page on the console, and you will know.

7. Window size operation

Common window operations include setting window size, maximizing window, minimizing window, and full-screen window.

① sets the window size

Driver.set_window_size (1920, 1080)

② maximize window

Driver.maximize_window ()

③ minimization window

Driver.minimize_window ()

Minimizing windows is a new feature of selenium4, and selenium3 cannot use this method

④ full screen window, which is equivalent to pressing F11 in most browsers

Driver.fullscreen_window ()

Sample code:

From selenium import webdriverdriver = webdriver.Edge () driver.get ('https://www.baidu.com/')driver.set_window_size(1920, 1080) # set window size 1920*1080driver.minimize_window () # minimize window driver.maximize_window () # maximize window driver.fullscreen_window () # full screen window

After this code opens the home page of Baidu, first set the size of the browser window to 1920-1080, and then minimize the window, maximize the window, and then the full-screen window. When actually testing, you need to set it as needed.

8. Quit

After the test execution is completed, you need to exit the browser, otherwise running the test many times will result in a large number of driver processes remaining in the system, which will consume computer resources and cause the system to become more and more stuck, so it is a good habit to close the browser after the test is completed.

The operation to exit the browser is as follows:

From selenium import webdriverfrom selenium.webdriver.common.by import Bydriver = webdriver.Edge () driver.get ('https://www.baidu.com/')driver.find_element(By.ID,' kw'). Send_keys ('selenium') # search box enter seleniumdriver.find_element (By.ID,' su'). Click () # Click Baidu driver.quit () # to exit the browser

This code means to open the home page of Baidu, enter selenium in the input box, click Baidu, and exit the browser after completing the search.

The above is about the content of this article on "how to operate the browser in python". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

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

Development

Wechat

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

12
Report