Automated Test Selenium Module Webdriver use

Source: Internet
Author: User
Tags xpath

First, Webdriver basic use of commands
From selenium import webdriver   # Imports Webdriver module >>> chrome_obj = Webdriver. Chrome ()              # open Google Chrome >>> chrome_obj.get ("https://www.baidu.com")      # Open URL >>> chrome_ Obj.get (r "C:\desktop\text.html")      # Open the local HTML page >>> chrome_obj.title          # Get the name of the open URL >>> chrome_ Obj.current_url    # Gets the URL of the open URL

>>> chrome_obj.close () #关闭浏览器窗口

  

Second, label navigation

General positioning label
# Find label >>> label = chrome_obj.find_element_by_id ("kw") >>> label = Chrome_obj.find_element_by_name (" WD ") >>> label = Chrome_obj.find_element_by_class_name (" S_ipt ") >>> label = Chrome_obj.find_element_ By_tag_name ("Imput") >>> label = Chrome_obj.find_element_by_link_text ("Content in a label accurate positioning")  >>> Label = Chrome_obj.find_element_by_partial_link_text ("content blur positioning in a label") >>> label = Chrome_obj.find_element_by_ XPath ("Add regular CSS path in copy tag") >>> label = Chrome_obj.find_element_by_css_selector ("input=[id= ' id_name '/name= ' Name_name '/....../] ")

  

Label navigation XPath tag positioning complex case consider using XPath

XPath is the XML Path language, which is a language used to determine the location of a portion of an XML (a subset of standard generic Markup Language) documents. XPath is an XML-based tree structure that has different types of nodes, including element nodes, attribute nodes, and text nodes, providing the ability to find nodes in a data structure tree.

#Absolute Path>>> label = Chrome_obj.find_element_by_xpath ("Html/boday/p/input")#Absolute Path Navigation>>> label = Chrome_obj.find_element_by_xpath ("Html/boday/p/input[1]")#absolute Path navigation, multiple input boxes, determining the first input box#relative Path>>> label = Chrome_obj.find_element_by_xpath ("//input")#relative path navigation means that the input tag in the entire document defaults to the first * first "//" representation in the entire document>>> label = Chrome_obj.find_element_by_xpath ("//input[2]")#The second input box in the specified page does not have an error#child node found under parent node>>> label = Chrome_obj.find_element_by_xpath ("//form//input")#//parent node//child node * Return child node input>>> label = Chrome_obj.find_element_by_xpath ("//form//input[2]")#//parent node//child node [2] * Specify the second input child node under the parent node#Locating parent nodes through child nodes>>> label = Chrome_obj.find_element_by_xpath ("//form//input/ .")#returns the parent node of input form label>>> label = Chrome_obj.find_element_by_xpath ("//form//input/.")#Current Node#Find nodes by property>>> label = Chrome_obj.find_element_by_xpath ("//input[@id]")#relative path navigation find all input tags with label with id attribute>>> label = Chrome_obj.find_element_by_xpath ("//input[@id = ' 1 ']")#Property Lookup Find the input tag with id=1 in all input tags>>> label = Chrome_obj.find_element_by_xpath ("//input[@name = ' Xiahua ')")  #Tag Statistics Countains>>> label = Chrome_obj.find_element_by_xpath ("//*[countains (Input) =1]")#//* represents all labels in the entire document, [count (input) =1] represents the input label of only one input sub-label under the parent tag>>>label = Chrome_obj.find_element_by_xpath ("//*[countains (Input) =2]")#//* represents all the labels in the entire document, [count (input) =1] represents the input label with two input sub-labels under the parent tag#local-name Fuzzy Lookup>>> label = Chrome_obj.find_element_by _xpath ("//*[local-name () = ' input ']")#finds all input tags in the current document by default returns the first>>> label = Chrome_obj.find_element_by _xpath ("//*input")#finds all input tags in the current document by default returns the first>>> label = Chrome_obj.find_element_by _xpath ("//*[local-name (), ' I '")#Find the label in the current document with the letter I in the name of the tag, such as input title>>> label = Chrome_obj.find_element_by _xpath ("//*[local-name (), ' I '")#finds all input tags in the current document by default returns the first>>> label = Chrome_obj.find_element_by _xpath ("//*[countains (Local-name (), ' I ')] [last ()]) # Find the final element of all the child tags of the label containing the letter "I" in the current document (a bit of a prick)>>> label = Chrome_obj.find_element_by _xpath ("//*[strint-length (Local-name () =3)] [last ()]) # finds all labels with a number of 5 characters in the current document, and formulates the final label. Title input (5 str) 
View Code

  

  

III. Simulation of user operation
>>> label.get_attribute ("type") # Displays the label's Type property name type ID placeholder>>> label.tag_name ()  # Get tag Name  
>>> #模拟鼠标悬浮
# Simulate A-tag  click event
>>> Label.send_keys("Simulation Search Content") # Analog Input Box entry
# Clear the Input tab

>>> Chrome_obj.back () # Simulation Browser returns to previous browse page

  

 

1. Analog Mouse operation
From selenium.webdriver.common.action_chains import actionchains #导入模块>>> label = Chrome_obj.find_ Element_by_link_text ("point I hover show other a label") >>> Actionchains (chrome_obj). Move_to_element (Label). Perform ()   # Simulate user hover    
"" "Actionchains (chrome_obj) is used to generate simulated user behavior;
Perform ()  Execute storage Behavior "" "

>>> Label_bel = Chrome_obj.find_element_by_link_text ("I am a tag, point me page Jump")
>>> Label_bel.click () # Simulate user clicks

Other mouse actions

Label.countext_lick () # Right-click Label.double_click () # Double-click Label.drag_and_drop () # drag label.move_to_element  # Hover Label.click_and_hold  # Press the left mouse button to keep moving

  

2. Analog keyboard operation
From Selenium.webdriver.common.keys import keys  #  Introduction module >>> label.send_keys ("Input content") >> > Label.send_keys (keys.back_spance)  # backspace >>>label.send_keys (Keys.contrl, ' a ')  # Full selection >>> Label.send_keys (Keys.contrl, ' V ')  # paste >>>label.send_keys (Keys.contrl, ' C ')  # copy >>> Label.send_keys (Keys.contrl, ' X ') # Cut >>>label.send_keys (keys.enter)   # Enter

  

Iv. Processing dialog boxes

Python Script for automatic login

Automated Test Selenium Module Webdriver use

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.