Python+selenium 5-The first complete automated test script __python

Source: Internet
Author: User
Tags xpath

In the previous article, we introduced how to use XPath expressions to locate page elements, and after mastering how to crawl or write an exact XPath expression, we can start writing our first truly WebUI automated test script, which is equivalent to learning python, How to print Hello,python on the console. The same.

Our test cases are: Open Baidu home page, search selenium, and then check the search list, there is no selenium this website link options.

Before you write an automated script, you need to define the steps of the manual script and then split it into concrete steps to do nothing, think about it, and start writing the script. I split the test scenario into the following steps:

1 After starting the browser, here we use Chrome

2 Open Baidu homepage, https://www.baidu.com

3 Locate the Search input box, record the XPath expression of the input box element://*[@id = ' kw ']

4 Position Search Submit button (Baidu Click this button), get XPath expression://*[@id = ' su ']

5 in the Search input box input "Selenium", click Baidu This button.

6 in the Search results list to determine whether there is a Selenium official website this link.

7 exit the browser and end the test.


If you don't get the element's expression through the plugin Firepath on Firefox, see an article. If you don't have Chrome installed, use Firefox.

1 Desktop A new Notepad, named and saved as firstscript.py

2 Enter the following code fragment to firstscript.py, save

3 Open the cmd window, switch to the desktop path, and then execute the script via Python firstscript.py.


The firstscript.py script is as follows

# coding=utf-8
import time from
Selenium import webdriver

driver = webdriver. Chrome () # Open Chrome, if Chrome is not installed, replace with Webdriver. Firefox ()
Driver.maximize_window ()    # Maximize browser window
driver.implicitly_wait (8)   # Set implicit time wait

Driver.get ("https://www.baidu.com")  # Address bar input Baidu address
Driver.find_element_by_xpath ("//*[@id = ' kw ']"). Send_keys ("Selenium")  # Search Input Box enter selenium
driver.find_element_by_xpath ("//*[@id = ' su ']"). Click ()  #点击百度一下按钮

# import time module, wait 2 seconds

Time.sleep (2) 
# Here, the element XPath expression is used to determine that the element is displayed in the list of results, thus judging the Selenium official website this link is displayed in the results list.
# Here we use the relative element positioning method/. /
# By Selenium Method is_displayed () to determine whether our target element is displayed on the page.
Driver.find_element_by_xpath ("//div/h3/a[text") = ' official website ']/. /a/em[text () = ' Selenium ']). is_displayed () driver.quit ()


The second type of assertion:

# coding=utf-8
import time from
Selenium import webdriver

driver = webdriver. Chrome ()
Driver.maximize_window ()
driver.implicitly_wait (8)

driver.get ("https://www.baidu.com")
Driver.find_element_by_xpath ("//*[@id = ' kw ']"). Send_keys ("Selenium")
Driver.find_element_by_xpath ("//* [@id = ' su '] "). Click ()

time.sleep (2)
# The second method of judging
ele_string = Driver.find_element_by_xpath ("//div/h3/a[ Text () = ' official website ']/. /a "). Text
if (ele_string = u" selenium-web Browser Automation "):
    print" The test succeeds and the result matches the expected result. " "
Driver.quit ()

Here only use two equal sign (= =) to determine whether two strings are exactly the same, sometimes we also need to get the string to cut operations, in order to go to match, later introduced string cutting processing in the automated test results of the use of judgment.

Summary: The hardest part of automated testing is how to write assertions, and how to determine whether test results are passed.

Suggestion: The above code includes the script example in later article, you still had better go to a line to copy, do not duplicate directly, stick to your notepad. Because, you are writing the script process, will certainly make some syntax and input errors, in the debug script process, by observing the error message until you solve the problem, you can successfully execute the script, you will get and reap a lot.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.