This article is participating in Python Theme Month. See the link for details

Today, LEARNING something simple and monotonous, Selenium eight positioning.

Eraser has several friends in the group said: alas, eraser, how do you write basic things. The eraser wanted to answer: this is nonsense, you call a product every day to write technology, appropriate.

Eight positions of Selenium

Every time I write eight positions, I always feel that I want to write martial arts novels. Eight bronze figures appear in front of me.

In Selenium, location is based on the tags or attributes of HTML page elements, described in the language of testing.

  1. Locate page elements on the web page, and get element objects;
  2. Click, drag, and input elements.

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

Location by ID

Any tag or element in an HTML page can have a unique ID. Therefore, an element can be located by ID on any page, provided that there is a tag with such an ID.

The method of locating by ID is find_element_by_id, which is a very good name, and finding tags by ID, as you’ll learn later in this series of methods, which are all similar in length.

from selenium import webdriver
import time

driver = webdriver.Firefox()
# open_driver = webdriver.Chrome()
# open_driver = webdriver.PhantomJS()
# Open Baidu Pictures
driver.get('https://image.baidu.com/')

# Find the input box and enter the model
driver.find_element_by_id("kw").send_keys("Model")

# driver.close()
Copy the code

The.find_element_by_id(“kw”) in the above code is looking for tags by ID.

When the code runs, open the browser and enter the model automatically.

Locate by name

Name is also an attribute of an HTML page element. You can search the source code of the page for name= to see where it is included. If you know a lot about the page, this step can also be ignored.

Locating by name uses the find_element_by_name method. It is found that the input box also carries the name attribute in baidu picture web page. The same operation as ID can be done with the following code.

from selenium import webdriver
import time

driver = webdriver.Firefox()
# open_driver = webdriver.Chrome()
# open_driver = webdriver.PhantomJS()
# Open Baidu Pictures
driver.get('https://image.baidu.com/')

# Find the input box by ID and enter the model
# driver.find_element_by_id("kw").send_keys(" model ")
Find the input box by name and enter the model
driver.find_element_by_name("word").send_keys("Little girl")

# driver.close()
Copy the code

Pay special attention to locating the page element with name. Make sure that name is unique in the page to be positioned.

Locate by class

The location method is find_element_by_class_name, which has a long name but is used in the same way as above.

Use class name to locate
driver.find_element_by_class_name("s_ipt").send_keys("Handsome boy")
Copy the code

Locate by link_text

This method is based on the matching text of the hyperlink label, such as the hyperlink in the red box in the following figure. Open Baidu pictures, click hd animation.

After you find the link, you can also jump to it. Note that the hyperlink text should be complete, i.e., complete match.
driver.find_element_by_link_text("Anime hd").click()
Copy the code

Figure look good

Positioning through partial_link_TEXT is a sub-level of link_text, similar to fuzzy matching, and the code can be written.

After you find the link, you can jump to it
driver.find_element_by_partial_link_text("高清").click()
Copy the code

Locate by tag_name

Tag_name is the tag name, which is located by the tag name in the HTML page.

# Locate by tag name, which is rarely used
form_div = driver.find_element_by_tag_name("form")
print(form_div)
Copy the code

This method is rarely used in web pages because of the high probability of duplicate label names.

Locate the fault using the CSS

The advantages of CSS positioning are speed and simple syntax. The difficulty is that you need to be familiar with CSS selectors, otherwise you need to supplement your KNOWLEDGE of HTML+CSS. The core method used in this post is cSS_selector, such as modifying the first piece of code to locate the id selector in the CSS.

# id selector
driver.find_element_by_css_selector("#kw").send_keys("Little friend")
# class selector
driver.find_element_by_css_selector(".s_ipt").send_keys("Little friend")
# name selector
driver.find_element_by_css_selector("input[name='word']").send_keys("Little friend")
Copy the code

Locate using XPath

XPath positioning is easier to use, but the trouble is that you need to learn an extra syntax, which is more flexible than CSS positioning, but slightly slower than CSS positioning (which is not always obvious).

driver.find_element_by_xpath("//*[@name='word']").send_keys("Big friend")
Copy the code

This blog series will not cover XPath syntax, but stick to the idea that you can do it yourself.

Write in the back

This blog focuses on eight Selenium location-based methods that you can use to master them, so don’t worry about learning them.

.