Seenium's Eighth Location Technology

I. Summary

Location mode Method
id location find_element_by_id()
name Location find_element_by_name()
class Location find_element_by_class_name()
tag location find_element_by_tag_name()
link location find_element_by_link_text()
Parti_link Location find_element_by_partial_link_text()
xpath location find_element_by_xpath()
css positioning find_element_by_css_selector()
Location mode Advantages and disadvantages
id Elements are used when the current page has a unique id
name Elements are used on the current page when they have a unique id ()
tag /
link Locating Text Connection
Parti_link Location Fuzzy Matching Text Hyperlink
xpath Label Hierarchical Location
css When CSS locates multiple class_names, it can be replaced by css_selector. Each class_name is preceded by [.] and the spaces are removed.

1. ID location: find_element_by_id()
In the search box attribute, there is an id="kw" attribute, through which we can locate the search box.

# coding = utf-8
from time import sleep
from selenium import webdriver

# Driver file path
driverfile_path = r'D:\coship\Test_Framework\drivers\chromedriver.exe'
# Start the browser
driver = webdriver.Chrome(executable_path=driverfile_path)
# Open Baidu Home Page
driver.get(r'https://www.baidu.com/')
# Locate the search box by id and enter selenium
driver.find_element_by_id('kw').send_keys('selenium')
# Wait 5 seconds
sleep(5)
# Sign out
driver.quit()

2. Name Location: find_element_by_name()
In the search box attribute, there is a name="wd" attribute. We can locate the search box by this name.

# coding = utf-8
from time import sleep
from selenium import webdriver

# Driver file path
driverfile_path = r'D:\coship\Test_Framework\drivers\chromedriver.exe'
# Start the browser
driver = webdriver.Chrome(executable_path=driverfile_path)
# Open Baidu Home Page
driver.get(r'https://www.baidu.com/')
# Locate the search box by name and enter selenium
driver.find_element_by_name('wd').send_keys('selenium')
# Wait 5 seconds
sleep(5)
# Sign out
driver.quit()

3. Class location: find_element_by_class_name()

From the search box attribute located above, there is a class="s_ipt" attribute, through which we can locate the search box.

# coding = utf-8
from time import sleep
from selenium import webdriver

# Driver file path
driverfile_path = r'D:\coship\Test_Framework\drivers\chromedriver.exe'
# Start the browser
driver = webdriver.Chrome(executable_path=driverfile_path)
# Open Baidu Home Page
driver.get(r'https://www.baidu.com/')
# Locate the search box by class and enter selenium
driver.find_element_by_class_name('s_ipt').send_keys('selenium')
# Wait 5 seconds
sleep(5)
# Sign out
driver.quit()

4. tag location: find_element_by_tag_name()

If we understand HTML, we know that HTML defines functions through tags, such as input, table, etc. Each element is actually a tag. A tag is often used to define a kind of function. When we look at the HTML code of Baidu homepage, we can see many tags such as div,input,a, etc. So it is difficult to distinguish different elements by tag. Basically, we don't use this definition method in our work, just understand it. The following code is for reference only. Errors must be reported at runtime.

# coding = utf-8
from time import sleep
from selenium import webdriver

# Driver file path
driverfile_path = r'D:\coship\Test_Framework\drivers\chromedriver.exe'
# Start the browser
driver = webdriver.Chrome(executable_path=driverfile_path)
# Open Baidu Home Page
driver.get(r'https://www.baidu.com/')
# Locate the search box by tag and enter selenium, where errors must be reported
driver.find_element_by_tag_name('input').send_keys('selenium')
# Wait 5 seconds
sleep(5)
# Sign out
driver.quit()

5. Link Location: find_element_by_link_text()

This method is specially used to locate text links, such as "news", "hao123", "map" and other links in the upper right corner of Baidu homepage.

# coding = utf-8
from time import sleep
from selenium import webdriver

# Driver file path
driverfile_path = r'D:\coship\Test_Framework\drivers\chromedriver.exe'
# Start the browser
driver = webdriver.Chrome(executable_path=driverfile_path)
# Open Baidu Home Page
driver.get(r'https://www.baidu.com/')
# Locate the "News" link through link and click
driver.find_element_by_link_text('Journalism').click()
# Wait 5 seconds
sleep(5)
# Sign out
driver.quit()

6. Parti_link Location: find_element_by_partial_link_text()

Sometimes the text of a hyperlink is very long. If we input all of it, it is not only troublesome, but also very beautiful. At this time, we can intercept only a part of the string and use this method to fuzzily match.

We use this method to locate the "news" hyperlink on Baidu homepage.

# coding = utf-8
from time import sleep
from selenium import webdriver

# Driver file path
driverfile_path = r'D:\coship\Test_Framework\drivers\chromedriver.exe'
# Start the browser
driver = webdriver.Chrome(executable_path=driverfile_path)
# Open Baidu Home Page
driver.get(r'https://www.baidu.com/')
# Locate the "News" link through partial_link and click
driver.find_element_by_partial_link_text('smell').click()
# Wait 5 seconds
sleep(5)
# Sign out
driver.quit()

7. XPath location: find_element_by_xpath()

Several positioning methods mentioned above are in ideal state and have a certain scope of use. That is, in the current page, each element has a unique attribute of id or name or class or hyperlink text, so we can locate them through this unique attribute value.

But it's not so good in practice. Sometimes the elements we want to locate don't have id,name,class attributes, or the values of these attributes of multiple elements are the same, or when we refresh the page, the values of these attributes will change. At this point, we can only locate it through xpath or CSS.

# coding = utf-8
from time import sleep
from selenium import webdriver

# Driver file path
driverfile_path = r'D:\coship\Test_Framework\drivers\chromedriver.exe'
# Start the browser
driver = webdriver.Chrome(executable_path=driverfile_path)
# Open Baidu Home Page
driver.get(r'https://www.baidu.com/')
# Locate the search box through xpath and enter selenium
driver.find_element_by_xpath("//*[@id='kw']").send_keys('selenium')
# Wait 5 seconds
sleep(5)
# Sign out
driver.quit()

8. CSS Location: find_element_by_css_selector()

This method is simpler and faster than xpath, but it will be difficult to understand. Here is a brief introduction.

CSS Location Baidu Search Box

# coding = utf-8
from time import sleep
from selenium import webdriver

# Driver file path
driverfile_path = r'D:\coship\Test_Framework\drivers\chromedriver.exe'
# Start the browser
driver = webdriver.Chrome(executable_path=driverfile_path)
# Open Baidu Home Page
driver.get(r'https://www.baidu.com/')
# Locate the search box through CSS and enter selenium
driver.find_element_by_css_selector('#kw').send_keys('selenium')
# Wait 5 seconds
sleep(5)
# Sign out
driver.quit()

Tags: Selenium Attribute

Posted on Thu, 29 Aug 2019 01:02:20 -0700 by gortron