A background.

Selenium is a tool for Web application testing. Selenium tests run directly in the browser, just as real users do. Supported browsers include Internet Explorer (7, 8, 9, 10, 11), Mozilla Firefox, Safari, Google Chrome, Opera, and more. The main functions of this tool include: testing browser compatibility – testing your application to see if it works well on different browsers and operating systems. Test system functionality – Create regression tests to verify software functionality and user requirements. Supports automatic recording of actions and automatic generation of test scripts in.net, Java, Perl and other languages.

Advantages:
  • The bottom layer of the framework uses JavaScript to simulate the operation of the browser by real users. When the test script is executed, the browser automatically clicks, enters, opens, and validates the script code, just as a real user would, testing the application from the end user’s perspective.
  • Makes it possible to automate browser compatibility testing, although there are still subtle differences between browsers.
  • Simple to use, you can use Java, Python and other languages to write use case scripts.
Important links:

Official GitHub address: github.com/SeleniumHQ/… PyPI: pypi.org/project/sel… Chinese document of Selenium – Python: Selenium – Python – useful. Readthedocs. IO/en/latest /

Install Selenium

The first step is to install a version of Python. Python3.x is recommended, as Python2 will no longer be maintained in 2020. The second step is to add the Python installation directory to the environment variable. There are a lot of tutorials on the Internet, here is no longer redundant. If you have PIP on your system, you can simply install or upgrade the Python binding:

pip install selenium
Copy the code

Selenium3 Browser driver

When Selenium was upgraded to 3.0, different browser drivers were standardized. If you want to use Selenium to drive different browsers, you must download and set up different browser drivers separately.

  • Chrome driver: Chrome Driver
  • Firefox driver: Geckodriver
  • IE browser driver: IEDriverServer
  • Other browser drivers search the web themselves.

You are advised to use Chromedriver. exe for Chrome. The corresponding Chrome version must be correct, otherwise it will not run. Once the browser driver is downloaded, it also needs to be added to the environment variables. It is recommended to mv directly to /usr/local/bin, or to the Python installation root, since these addresses are already added to the environment variables.

Four. Simple use

Programming language learning from Hello Word entry. Our Learning in Selenium is no exception. Here is a simple Hello Word file to start learning. Open a Python editor. By default, Python’s own IDLE will work. To create the hello.py file, enter the following:

from selenium import webdriver          Import the browser driver

driver = webdriver.Chrome()             # initialization
driver.get('https://www.baidu.com')     # Open baidu home page
print(driver.title)                     Print the page title
driver.quit()                           # Close the browser
Copy the code

At this point, if you can successfully open your browser, congratulations.

5. Selenium element location

Selenium provides eight locating methods: ID, Name, Class Name, Tag Name, Link Text, Partial Link Text, xpath, and CSS selector

The corresponding methods in Python selenium are as follows: locating by ID: find_element_by_id() locating by name: find_element_by_name() locating by class: Find_element_by_class_name () locates by tag: find_element_by_tag_name() locates by link: Find_element_by_link_text () is located by partial_link: find_element_by_partial_link_text() is located by xpath: Find_element_by_xpath () via CSS: find_element_by_css_selector()

How to use it? For example, suppose a Web page element has attributes that look like this:

<html>
  <head>
  <body link="#0000cc">
    <a id="result_logo" href="/" onmousedown="return c({'fm':'tab','tab':'logo'})">
    <form id="form" class="fm" name="f" action="/s">
      <span class="soutu-btn"></span>
        <input id="kw" class="s_ipt" name="wd" value="" maxlength="255" autocomplete="off">
Copy the code
  • Locating by ID:
dr.find_element_by_id("kw")
Copy the code
  • Locate by name:
dr.find_element_by_name("wd")
Copy the code
  • Locate by class name:
dr.find_element_by_class_name("s_ipt")
Copy the code
  • Locating by tag name:
dr.find_element_by_tag_name("input")
Copy the code
  • Xpath localization. There are N ways to write xpath localization. Here are some common ways to write xpath localization:
dr.find_element_by_xpath("//*[@id='kw']")
dr.find_element_by_xpath("//*[@name='wd']")
dr.find_element_by_xpath("//input[@class='s_ipt']")
dr.find_element_by_xpath("/html/body/form/span/input")
dr.find_element_by_xpath("//span[@class='soutu-btn']/input")
dr.find_element_by_xpath("//form[@id='form']/span/input")
dr.find_element_by_xpath("//input[@id='kw' and @name='wd']")
Copy the code
  • Location using the CSS. There are N ways to write the LOCATION using the CSS. Here are some common ways:
dr.find_element_by_css_selector("#kw")
dr.find_element_by_css_selector("[name=wd]")
dr.find_element_by_css_selector(".s_ipt")
dr.find_element_by_css_selector("html > body > form > span > input")
dr.find_element_by_css_selector("span.soutu-btn> input#kw")
dr.find_element_by_css_selector("form#form > span > input")
Copy the code

Next, we have a set of text links on our page.

<a class="mnav" href="http://news.baidu.com" name="tj_trnews"> </a> <a class="mnav" href="http://www.hao123.com" name="tj_trhao123">hao123</a>
Copy the code
  • Location via link text:
dr.find_element_by_link_text("News")
dr.find_element_by_link_text("hao123")
Copy the code
  • Location via partial Link Text:
dr.find_element_by_partial_link_text("New")
dr.find_element_by_partial_link_text("hao")
dr.find_element_by_partial_link_text("123")
Copy the code

The positioning of XPAHT and CSS is complicated. For details, see:

  • Xpath syntax
  • CSS selectors

Vi. Control browser operations

Control the browser window size Sometimes we want to open at a certain browser size so that the page we visit will run at that size. For example, you can set the browser to mobile size (480* 800) and then visit the mobile site to evaluate its style. WebDriver provides the set_WINDOW_size () method to set the browser size.

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("http://m.baidu.com")

# Parameter number is pixel point
print("Set browser width 480, height 800 display")
driver.set_window_size(480, 800)
driver.quit()
Copy the code

Most of the time when you want the browser to run in full-screen mode when you run an automated test script on the PC side, you can use the maximize_window() method to make the browser open in full-screen mode. This is the same as set_window_size(), but it takes no arguments.

2. Control the browser back and forward buttons when browsing web pages using the browser, the browser provides back and forward buttons, which can easily switch between browsing web pages. WebDriver also provides corresponding back() and forward() methods to simulate the back and forward buttons. The following examples demonstrate the use of both methods.

from selenium import webdriver

driver = webdriver.Chrome()

# Visit baidu home page
first_url= 'http://www.baidu.com'
print("now access %s" %(first_url))
driver.get(first_url)

# Visit the news page
second_url='http://news.baidu.com'
print("now access %s" %(second_url))
driver.get(second_url)

Go back to baidu home page
print("back to %s "%(first_url))
driver.back()

# Go to the news page
print("forward to %s"%(second_url))
driver.forward()

driver.quit()
Copy the code

To see the script in action, print() for each step to print the current URL.

3. Page refresh Sometimes you need to manually refresh the page (F5).

driver.refresh() Refresh the current page...Copy the code