In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to write python automated test scripts". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to write python automated test scripts.
Create a webdriver object
Use the following code to create a webdriver object for the next operation.
Driver = webdriver.Chrome ()
Start with the positioning element
There are eight positioning methods for selenium, which are id positioning, name positioning, class positioning, tag positioning, link positioning, partial link positioning, css positioning and Xpath positioning. Among them, Xpath positioning is widely used. The editor then uses Xpath positioning to demonstrate:
Driver.find_element_by_xpath ("/ / * [@ id='nav'] / div/ul/li [2]") .click ()
There is a find_element_by_xpath method under driver to get a html element, which you can click using the click () method after getting this element. Xpath can be selected in the find_element_by_xpath method. For tutorials on Xpath, please refer to the Xpath tutorial.
Browser control
Browser control directly calls the method of the driver object to control the browser.
# Control window size driver.set_window_size (480800) # set hibernation in seconds sleep (2) # control page back driver.back () sleep (2) # control page forward driver.forward () sleep (2) # control page refresh driver.refresh () # maximize page driver.maximize_window ()
Keyboard control
After the element is selected, you can use send_keys () to type the specified content into the target element (typically used to enter data).
# element operation-simulate input (navigate to the input box), send the input data driver.find_element_by_xpath ("/ / * [@ id='searched_content']") .send_keys ("java tutorial") through send_keys
Mouse control
You can use the ActionChains object to perform mouse operations, and add the perform () method to execute the temporary mouse operations after the mouse operation.
# # use right_ to store the selected element object right_click = driver.find_element_by_xpath ("/ / * [@ id='searched_content']") # using the following code, you can right-click the right_ object ActionChains (driver). Context_click (right_click). Perform () move = driver.find_element_by_xpath ('/ html/body/div [4] / div') # use the following code You can hover over a move object ActionChains (driver). Move_to_element (move). Perform ()
All of the above code synthesis
The above code is not the full version, the following is the full version of the code, including references to the selenium library and other code.
From selenium import webdriver# uses selenium to import selenium packages from selenium.webdriver.common.action_chains import ActionChains# uses mouse click events to import ActionChainsfrom time import sleep# uses sleep to import time packages # creates a webdriver object driver = webdriver.Chrome () # accesses w3Cschooldriver.get using webdriver objects ("https://www.yisu.com/")# uses the Xpath method to get the location of an element on the page Use the click method to set hibernation by clicking on the element driver.find_element_by_xpath ("/ / * [@ id='nav'] / div/ul/li [2]") .click () # Control window size driver.set_window_size (480800) # Sleep (2) # Control Page back driver.back () sleep (2) # Control Page forward driver.forward () sleep (2) # Control Page Refresh driver.refresh () # maximize Page driver.maximize_window () # element Operation-Analog input (navigate to input box) Send input data driver.find_element_by_xpath ("/ / * [@ id='searched_content']") .send_keys ("java tutorial") # element operation through send_keys Clear the text sleep (2) driver.find_element_by_xpath ("/ / * [@ id='searched_content']") .clear () # # use right_ to store the selected element object right_click = driver.find_element_by_xpath ("/ / * [@ id='searched_content']") # use the following code You can right-click the right_ object ActionChains (driver). Context_click (right_click). Perform () move = driver.find_element_by_xpath ('/ html/body/div [4] / div') # using the following code, you can hover over the move object ActionChains (driver). Move_to_element (move). Perform () so far, I believe you have a deeper understanding of "how to write python automated test scripts". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.