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 write python programming

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

Share

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

Python programming how to use selenium to write, for this problem, this article details the corresponding analysis and solution, hoping to help more small partners who want to solve this problem find a simpler and easier way.

from selenium import webdriver class TestBaiDu(): def setup(self): self.driver=webdriver.Chrome() self.driver.maximize_window() self.driver.implicitly_wait(5) def teardown(self): self.driver.quit() def test_baidu(self): self.driver.get("https://www.baidu.com/") self.driver.find_element_by_css_selector("area").click() handle=self.driver.window_handles self.driver.switch_to.window(handle[-1]) el=self.driver.find_element_by_class_name('t') el.click() assert 'Baidu Hot Search Official'==el.text

The sample code is a simple test case written using pytest+selenium, which mainly implements the following operations:

Step 1: Open Baidu

Step 2: Click the Baidu icon on the page,

Step 3: Switch to another window

Step 4: Find "Official Hot Search" and click

Step 5: Asserting

From the sample code, we need to know that writing test cases using pytest+selenium requires the following four key elements:

1. import dependencies from selenium import webdriver #import dependencies

Use this code to import the dependency we want to use.

In Selenium introduction and installation deployment above I introduced the principle of selenium, we need to use webdriver to drive different browsers, so we must import webdriver when writing test cases using selenium.

2. Create driverdef setup(self): self.driver=webdriver.Chrome() #Declare a chrome webdriver self.driver.maximize_window() Maximize the browser window self.driver.implicitly_wait(5) #implicitly wait

In the sample code, we use setup() and teardown() to perform the pre-execution and post-execution operations of the case, respectively. Before executing the test case, we need to declare drivers based on browser type, so we declare a chrome driver using self.driver=webdriver.Chrome() in setup().

As shown above, selenium also supports other types of browser drivers, which you can use according to your own needs.

3. def test_baidu(self): self.driver.get("https://www.baidu.com/") #Open URL self.driver.find_element_by_css_selector("area").click() #Locate element and click handle=self.driver.window_handles self.driver.switch_to.window(handle[-1]) #Switch window el=self.driver.find_element_by_class_name('t') el.click()

Here are two common operations:

Click()

When you navigate to an element, click on it using click().

Example 1:

find_element_by_id('kw').click()

Input: send_keys()

When we locate an element that needs input, send_keys() can be used to pass what we want to input to the element.

Example 2:

find_element_by_id('kw').send_keys("selenium")4. Assertion assert 'Baidu Hot Search Official'==el.text

After the test case is run, it is generally necessary to use assertions to determine whether the case is consistent with our expected results. Assert can be used to achieve this purpose.

About python programming how to use selenium to write the answer to the question shared here, I hope the above content can have some help for everyone, if you still have a lot of doubts not solved, you can pay attention to the industry information channel to learn more related knowledge.

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