Selenium notes 1

Source: Internet
Author: User
Tags xpath

#!/usr/bin/env python#-*-coding:utf-8-*-"""@author: https://shop60459643.taobao.com@contact: [email protected], https://shop60459643.taobao.com@others: Dtstudio, All rights reserved--Created on 2015/11/09@desc: Talk about the method properties commonly used in Web projects Webdriver provides eight ways to locate elements: ID, name, class Nam E, tag name, link text, Partial link text, XPath, CSS selector the corresponding positioning method in the Python language is as follows: find_element_by_id () find_element_by_ Name () Find_element_by_class_name () Find_element_by_tag_name () Find_element_by_link_text () find_element_by_partial _link_text () Find_element_by_xpath () Find_element_by_css_selector ( )""" fromSeleniumImportWebdriverImportOSImportSyspath=os.getcwd () path='file://'+ Path.split ('Test_case') [0] +'demo_html'+ Os.path.sep +'test.html'test_html= Path.decode ('GBK'). Encode ('Utf-8') Baidu_url='http://www.baidu.com'Dr=Webdriver. Firefox () dr.implicitly_wait (30) Dr.get (baidu_url)#Baidu search box of HTML#<input type= "text" class= "S_ipt" name= "wd" id= "kw" maxlength= "Up" autocomplete= "off" >#1. Based on the id attribute, id= "kw"DR.FIND_ELEMENT_BY_ID ('kw'). Clear () dr.find_element_by_id ('kw'). Send_keys ('python')#2. Based on the name attribute, name= "WD"Dr.find_element_by_name ('WD'). Clear () Dr.find_element_by_name ('WD'). Send_keys ('python')#3. Based on the class attribute, class= "S_ipt"Dr.find_element_by_class_name ('S_ipt'). Clear () Dr.find_element_by_class_name ('S_ipt'). Send_keys ('python')#4.find_element_by_tag_name (), the label of the search box is <input></input>#TIPS: tagname targeting is rarely used, because the page contains too many identical labels,#For example, the _testfile page contains 3 input elements, using find_element_by_tag_name (' input '),#driver is unable to tell which input you are going to operate on, here we use Find_elements_by_tag_name (' input ')#Notice the usage of find_elements_xxx here.dr.get (test_html) eles= Dr.find_elements_by_tag_name ('input') forEinchEles:ifE.get_attribute ('ID') =='username': E.send_keys (U'This is the User Name entry box')#5.find_element_by_link_text ()#<a onmousedown= "return Ns_c ({' fm ': ' Behs ', ' tab ': ' Tj_duty '} ') ' href= ' http://www.baidu.com/duty/' > use Baidu before must read </a>#the role of the plus U is to convert the Chinese string into Unicode encodingdr.get (Baidu_url) dr.find_element_by_link_text (U'use Baidu before must read'). Click ()#6.find_element_by_partial_link_text (), the partial link text is a supplement to link text. #just take a portion of the text link, and the following 2 lines of code are anchored to the same elementDr.back ()#back to previous pageDr.find_element_by_partial_link_text (U'use Baidu'). Click () Dr.back () Dr.find_element_by_partial_link_text (U'Baidu must read before'). Click ()#7.find_element_by_xpath ()Dr.back ()#7.1 Using absolute path XPath positioningDr.find_element_by_xpath ("/html/body/div[3]/div[1]/div/div[1]/div/form/span[1]/input"). Send_keys ('0')#7.2 XPath positioning using relative paths#As for the XPath technique, it is recommended that the parents focus on the study in the latter part of their studies. It is certainly possible to use xpath/css when the element cannot be positioned by conventional methods. #<input type= "text" class= "S_ipt" name= "wd" id= "kw" maxlength= "Up" autocomplete= "off" >Dr.find_element_by_xpath ("//input[@id = ' kw ')"). Send_keys ('1') Dr.find_element_by_xpath ("//input[@name = ' WD ']"). Send_keys ('2') Dr.find_element_by_xpath ("//input[@class = ' s_ipt ')"). Send_keys ('3') Dr.find_element_by_xpath ("//*[@class = ' s_ipt ')"). Send_keys ('4')#8.find_element_by_css_selector ()Dr.find_element_by_css_selector (". S_ipt"). Send_keys ('5')#the. Number followed by the class attribute, class= "S_ipt"Dr.find_element_by_css_selector ("#kw"). Send_keys ('6')##号后面跟的是id属性, id= "kw"#stay here for 30 seconds, Baidu input box input is 0123456ImportTimetime.sleep (30)#PrintU"the text of the search button is >>>", dr.find_element_by_id ('su'). Textdr.get (baidu_url) News_link= Dr.find_element_by_link_text (U"News")PrintU"News link is >>>", News_link.get_attribute ("href")PrintU"the Name property of the news link is >>>", News_link.get_attribute ("name")#9. Exit, there are 2 waysDr.quit ()#Close "All windows" and exit the associated driver,#Dr.close () # Close the current window and note the difference between the two#the difference between find_element_by_xxxx and find_elements_by_xxx:#find_element_by_xxxx: Positioning an element#find_elements_by_xxx: Locates a set of elements, gets a list, takes a value from the list, and then makes a specific operation on each element .

Selenium notes 1

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.