Python---positioning elements

Source: Internet
Author: User

1. Positioning elements: Baidu input box and search as an example

(1) ID locator: The id attribute is unique in an HTML document
The find_element_by_id () method locates elements through the id attribute
(2) Name locator
Find_element_by_name () method
(3) Class positioning
Find_element_by_class_name () method
(4) Tag positioning: tag is often used to define a class of functions, the probability of identifying an element through tag is very low
Find_element_by_tag_name () method
(5) Link positioning: Used to locate text links.

Find_element_by_link_txt ("News")

The Find_element_by_link_txt () method locates elements by using element tags for previous text information
(6) Partial link positioning: is a link positioning of a supplement, some text links longer, at this time need to take part of the text link positioning, only this part of the information can uniquely identify the link.

The Find_element_by_partial_link_text () method locates elements by partially textual information between the element label pairs

#coding =utf-8
From selenium import Webdriver
Driver = Webdriver. Firefox ()//Get Friefox Browser object, launch browser
Driver.get ("http://www.baidu.com";)//Enter URL into browser
driver.find_element_by_id ("kw"). Send_keys ("python")//Locate the Baidu input box and enter Python
driver.find_element_by_id ("su"). Click ()//position "Baidu button" to send a clicking event

#driver. Find_element_by_link_text ("News"). Click ()
#driver. Find_element_by_link_text ("hao123"). Click ()

driver.find_element_by_id ("kw"). Send_keys ("Python")
driver.find_element_by_id ("kw"). Clear ()
Driver.find_element_by_name ("WD"). Send_keys ("Python1")
‘‘‘
driver.find_element_by_id ("su"). Click ()
Driver.find_element_by_class_name ("S_ipt"). Send_keys ("Python")
Driver.find_element_by_class_name ("BG s_btn"). Click ()
Driver.find_element_by_tag_name ("Input"). Send_keys ("Python")
Driver.find_element_by_tag_name ("Input"). Click ()
Driver.quit ()//exit and close the browser and related drivers
‘‘‘

Python---positioning elements

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.