PYTHON-SELENUM3 fifth day positioning--infrequently used positioning and CSS positioning details

Source: Internet
Author: User
Tags tag name xpath

use tag to locate
Tag is labeled, not commonly used
For example: Baidu's input box tag is input
will eventually error, because Baidu Home input tag too much

driver = webdriver.Firefox(executable_path="d:\\geckodriver")driver.get("https://www.baidu.com")driver.find_element_by_tag_name("input").send_Keys("123")


Using link

Link hyperlink positioning, such as Baidu's hao123 href = "http://www.hao123.com

driver = webdriver.Firefox(executable_path="d:\\geckodriver")driver.get("https://www.baidu.com")driver.find_element_by_link_text("hao123").click()driver.back()driver.find_element_by_partial_link_text("123").click()

Explain the difference between link_text and Partial_link_text, sometimes the name is too long, partial_link is can be fuzzy positioning

CSS Positioning
As with XPath, CSS is based on the CSS derivation to locate the basic wording and CSS consistent with the wording such as:

The most common id,class, labels, take Baidu as an example
The CSS uses the # number to denote the id attribute, such as: #kw
CSS uses. Represents the class attribute, such as:. S_ipt
CSS directly with the tag name, without any identifiers, such as: input

driver = webdriver.Firefox(executable_path = "d:\\geckodriver")driver.get("https://www.baidu.com")driver.find_element_by_css_selector("#kw").send_keys("1")driver.find_element_by_css_selector(".s_ipt").send_keys("2")driver.find_element_by_css_selector("input").send_keys("3")

Other properties can be written directly
For example: The Name property and the AutoComplete property

driver.find_element_by_css_selector("[name=‘wd‘]").send_keys("4")driver.find_element_by_css_selector("[autocomplete=‘off‘]").send_keys("5")

Written by tag and element combination
For example:

driver.find_element_by_css_selector("input.#kw").send_keys("6")driver.find_element_by_css_selector("input.[id=‘kw‘]").send_keys("7")

Through level positioning

#xpath中的 xpath://form[@id=‘form‘]/span/input 和 //form[@class=‘fm‘]/span/input 用css来编写driver.find_element_by_css_selector("form#form>span>input").send_keys("8")driver.find_element_by_css_selector("form.fm>span>input").send_keys("9")

Index: Positioning

The same label can be positioned by Option:nth-child (1).

driver = webdriver.Firefox(executable_path="d:\\geckodriver")driver.get("file:///G:/%E6%A1%8C%E9%9D%A2/20180329/%E7%AC%AC%E4%BA%8C%E9%98%B6%E6%AE%B5/HTML/day8.html")driver.find_element_by_css_selector("select#zz>option:nth-child(1)").click()driver.find_element_by_css_selector("select#zz>option:nth-child(2)").click()driver.find_element_by_css_selector("select#zz>option:nth-child(3)").click()

Logical Operation positioning
Multiple elements can be positioned at the same time, enhanced positioning, and XPath is not the same, can not write and

driver = webdriver.Firefox(executable_path = "d:\\geckodriver")driver.get("https://www.baidu.com")driver.find_element_by_css_selector("input[name=‘wd‘][autocomplete=‘off‘]").send_keys("1")

Unfortunately, there is no fuzzy matching for CSS now.

PYTHON-SELENUM3 fifth day positioning--infrequently used positioning and CSS positioning details

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.