Python + selenium_ element positioning

Selenium 2.0 adds webdrive, which uses the browser's native API to encapsulate some underlying operation functions and supports python, java, and php.
Selenium principle: three-step
code request is sent to the browser driver, and the
driver is like a taxi driver Parsed our needs and
opened the browser to send us to the destination

What role does webdriver play: It
is one of the three cores of selenium, and it encapsulates the functions of the browser operation, so that we can directly call and realize the control of the browser.

1. Basic knowledge of python, not to mention, please buy a copy of "Python programming from entry to practice" to master it yourself.
Involved content: build environment, variables, data types, lists, operation lists, if statements, dictionaries, user input and while loops, functions, classes, files and exception handling, test code

2. Element positioning is located in
Selenium according to the attributes of the HTML page element. The operation steps in the web are as follows:
(1) Position the page element, get the element
(2) Perform operations on the element: click, double-click, drag, enter values, etc.

Selenium provides 8 positioning methods, which are id, name, xpath, class name, tag name, link_text, partial link text and css selector


The id value of the id positioning HTML tag is unique, so there is no id positioning multiple elements. If you can use id, you don’t need others, which increases maintenance costs. For example:
enter python on Baidu homepage, after execution:

#coding=utf-8
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
driver.find_element_by_id("kw").send_keys("python")

css positioning
css is mainly located by id and class. It has more than a dozen positioning selectors. Advantages: fast speed, simple syntax, and really simple
css positioning with id:

driver.find_element_by_css_selector("#kw").send_keys("python")

css uses class positioning:

driver.find_element_by_css_selector(".s_ipt").send_keys("python")

Other attribute positioning:

driver.find_element_by_css_selector('[name="wd"]')

Compound positioning:

driver.find_element_by_css_selector('[angwox="wd"][autocomplete="off"]')

Fuzzy positioning:

# 定位style值以tfjiao开头的元素
driver.find_element_by_css_selector('[style^="tfjiao"]').click()
# 定位style值以tengfei.gif结尾的元素
driver.find_element_by_css_selector('div[style$="tengfei.gif"]').click()
# 定位style值包含tengfei的元素
driver.find_element_by_css_selector('div[style*="tengfei"]').click()

Right-click to copy:
select the element, right-click to copy the absolute path, it seems there is nothing to say
Copy and paste: #form> input[type=hidden]:nth-child(3)

The link_text
value is: all the name news of the hyperlink

#coding=utf-8
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
driver.find_element_by_link_text("新闻").click()

xpth positioning
xpth is the abbreviation of xml path language, is a language used to determine the location of a certain part of the xml document. xpath is more flexible than css and slower than css
(1) locate interface text, this is a lot

driver.find_element_by_xpath('//*[text()="kw"]').click()

(2) Attribute value positioning

driver.find_element_by_xpath('//*[@xygsng="kw"]').send_keys("python")

(3) Just understand the compound positioning: use the combination of css

driver.find_element_by_xpath('//*[@xygsng="kw" and @xydafn="mg"]').send_keys("python")

(4) Right-click copy full xpth [absolute path], there is really no way to use this again, anyway, right-click copy is sometimes unreliable, pit ah pit

driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[5]/div[1]/div/form/span[1]/input').send_keys("python")

partial_link_text is
similar to fuzzy query, this is nothing to say, but some hyperlink text is too long, it is more comfortable to use this
news

#coding=utf-8
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
driver.find_element_by_link_text("新").click()

name positioning

#coding=utf-8
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
driver.find_element_by_name("wd").send_keys("python")

Class positioning The
Baidu homepage search box as an example:

#coding=utf-8
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
**driver.find_element_by_class_name("s_ipt").send_keys("python")**

Tag_name positioning:
Basically not used, just look at it, locate by the name of the tag,

#coding=utf-8
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
print(driver.find_element_by_tag_name('form').get_attribute('name'))

Personal understanding of the eight major positioning:
1. CSS positioning id, name
2. Xpath positioning text value
3. CSS combination positioning
4. Xpath absolute path [Do not use maintenance, easy to go wrong, annoying]

Guess you like

Origin blog.csdn.net/weixin_45451320/article/details/112489722