Positioning element method

A positioning method element

        Official website address: http: //selenium-python.readthedocs.org/locating-elements.html
        There are various strategies for locating Web page elements (locate elements), you can choose the most suitable solution, Selenium provides the following method to define a page element:
find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector
        following is to find multiple elements (these methods will return a list):
find_elements_by_name
find_elements_by_xpath
find_elements_by_link_text
find_elements_by_partial_link_text
find_elements_by_tag_name
find_elements_by_class_name
find_elements_by_css_selector
        In addition to public methods given above, there are also two useful page object locator private methods. These two methods are find_element private and find_elements.
        The method is used by xpath positioned relative path, but also a good CSS method. For example:

<html>
<body>
<form id="loginForm">
<input name="username" type="text" />
<input name="password" type="password" />
<input name="continue" type="submit" value="Login" />
<input name="continue" type="button" value="Clear" />
</form>
</body>
<html>
        定位username元素的方法如下:
username = driver.find_element_by_xpath("//form[input/@name='username']")
username = driver.find_element_by_xpath("//form[@id='loginForm']/input[1]")
username = driver.find_element_by_xpath("//input[@name='username'] ")         [3] and attributes named name the first value of an input element username        [2] found by the first sub-element input id = loginForm form element values
        [1] the first element by a form input sub-elements, name attribute and the value achieved username

Second operating element method
        after the completion of about locating objects (locate elements) we need to operate on the targeted object, usually all operations will interact with the page by following WebElement interfaces, common operating element method:

clear Clears the content of the element
send_keys analog key input
click click elements
submit to submit the form
        , for example automatic access to FireFox browser automatically logged 163 mailboxes.


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

# Login 163 email
driver = webdriver.Firefox()
driver.get("http://mail.163.com/")

elem_user = driver.find_element_by_name("username")
elem_user.clear
elem_user.send_keys("15201615157")
elem_pwd = driver.find_element_by_name("password")
elem_pwd.clear
elem_pwd.send_keys("******")
elem_pwd.send_keys(Keys.RETURN)
#driver.find_element_by_id("loginBtn").click()
#driver.find_element_by_id("loginBtn").submit()
time.sleep(5)
assert "baidu" in driver.title
driver.close()
driver.quit()
        First of all by name to locate the user name and password, and then call the method clear () Clears the input box default content, such as "Please enter your password" and other tips by send_keys ( "**") to enter the correct user name and password, and finally through the click () Click login button or send_keys (Keys.RETURN) equivalent to Enter login, submit () submit the form.
        PS: If you need to enter the Chinese to prevent coding errors using send_keys (u "Chinese user name").

 

 

Three. WebElement interface to obtain value
        through WebElement interface can be used to obtain the values that are also very important.

Gets the size of the element size
text elements to obtain the text of
acquiring property value get_attribute (name)
LOCATION get coordinates of elements, first find the element you want to get, and then call the method
page_source return to the page source
driver.title return to the page title
current_url obtain the URL of the current page
is_displayed () set the element is visible
is_enabled () determines whether the element is used
is_selected () determines whether the element is selected
tag_name return element tagName
        example code is as follows:


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.PhantomJS(executable_path="G:\phantomjs-1.9.1-windows\phantomjs.exe")
driver.get("http://www.baidu.com/")

size = driver.find_element_by_name("wd").size
print size
#尺寸: {'width': 500, 'height': 22}

news = driver.find_element_by_xpath("//div[@id='u1']/a[1]").text
print news
#文本: 新闻

href = driver.find_element_by_xpath("//div[@id='u1']/a[2]").get_attribute('href')
name = driver.find_element_by_xpath("//div[@id='u1']/a[2]").get_attribute('name')
print href,name
#属性值: http://www.hao123.com/ tj_trhao123

driver.find_element_by_xpath = LOCATION ( "// div [@ ID = 'U1'] / A [. 3]") LOCATION.
Print LOCATION
# coordinates: { 'Y':. 19, 'X': 498}

Print driver.current_url
# current link: https://www.baidu.com/
Print driver.title
# title: Baidu, you know

the Result = LOCATION = driver.find_element_by_id ( "su") is_displayed ().
Print the Result
# is visible: True
        which image explained as shown below.

 

 

 

 


IV. Mouse
        in real automated testing is not just about the operation of the mouse click (), click operation, there are many operations included in ActionChains class. as follows:

context_click (elem) Right-click the mouse to click on the element elem, Save As and other acts
double_click (elem) Double-click the mouse to click on the element elem, map zoom function can be realized web
drag_and_drop (source, target) drag the mouse, press the left button to move the source element to the target release from
move_to_element (elem) to move a mouse over the element
click_and_hold (elem) press the left mouse button down over an element
perform () call to the function performed by the storage behavior ActionChains
        example shown below, the right mouse button to save acquisition Baidu picture logo. Code:
Import Time
from the webdriver Selenium Import
from selenium.webdriver.common.keys Import Keys
from selenium.webdriver.common.action_chains ActionChains Import

Driver = webdriver.Firefox ()
driver.get ( "http://www.baidu.com" )

# right mouse to move the picture to save the picture
elem_pic = driver.find_element_by_xpath ( "// div [@ the above mentioned id = 'LG'] / img")
Print elem_pic.get_attribute ( "src")
= ActionChains Action (Driver) .move_to_element (elem_pic)
action.context_click (elem_pic)

# Key: When right-clicking on the keyboard to move the cursor down to the first right-click menu option
action.send_keys (Keys.ARROW_DOWN)
the time.sleep (3 )
action.send_keys ( 'V') # Save as
action.perform ()

# Get Save as dialog box (failure)
alert.switch_to_alert ()
alert.accept ()
        results as shown below, by targeting xpath position and the right image click of the mouse, the pop-up menu, select "Save as picture." But how to click on the "Save As dialog box," the "Save" button is a difficult now just learning stage, not to the realm can not be solved. The reason:
        WebDriver CAN not Directly InterAct with the this IS Because Windows Dialog Dialog at The Domain of Windows are at The Operating System and not at The Webpage.

  

 

 

 


        The Recommended Resources section:
            the Selenium Right click to download the picture, combined with Sikuli - tobecrazy
            the Selenium WebDriver mouse and keyboard events analysis and extended
            Selenium Windows Save / Open Open Dialouge - StackOver
            book "selenium2 python automated testing" Author: Mushishi


Five keyboard.
        Reference: http: //selenium-python.readthedocs.org/api.html
        front on the mouse, now about the keyboard. Providing the key operation of the keyboard webdriver all the class Keys, of course, include some common operations, such as key combinations Ctrl + A (select all), Ctrl + C (copy), Ctrl + V (paste). More key official documents refer to the corresponding code.

send_keys (Keys.ENTER) press the Enter key
send_keys (Keys.TAB) press the Tab key to tab
send_keys (Keys.SPACE) Press the spacebar Space
send_keys (Kyes.ESCAPE) Press the Backspace key the Esc
send_keys (Keys. BACK_SPACE) press the delete key the BackSpace
send_keys (Keys.SHIFT) press the shift key
send_keys (Keys.CONTROL) pressing Ctrl
send_keys (Keys.ARROW_DOWN) press the mouse cursor down button
send_keys (Keys.CONTROL, 'a') Select key combination A + the Ctrl
send_keys (Keys.CONTROL, 'C') + C key combination to copy the Ctrl
send_keys (Keys.CONTROL, 'X') + X-key combination cut the Ctrl
send_keys (Keys.CONTROL, 'V') key combination Ctrl + V to paste
        the example used here Mushishi reference book "selenium2 python automated testing", recommend the book to everyone. Code is very interesting, we all feel to it ~

Coding. 8 = UTF-#
Import Time
from the webdriver Selenium Import
from selenium.webdriver.common.keys Keys Import

Driver = webdriver.Firefox ()
driver.get ( "http://www.baidu.com")

# input box content
driver.find_element_by_id = elem ( "kw")
elem.send_keys ( "Eastmount CSDN")
the time.sleep (3)

# CSDN delete a character Backspace key
elem.send_keys (Keys.BACK_SPACE)
elem.send_keys (Keys.BACK_SPACE)
elem .send_keys (Keys.BACK_SPACE)
elem.send_keys (Keys.BACK_SPACE)
the time.sleep (3)

# to enter a space + "blog"
elem.send_keys (Keys.SPACE)
elem.send_keys (U "blog")
the time.sleep (3 )

# Ctrl + A select all content input box
elem.send_keys (Keys.CONTROL, 'A')
the time.sleep (. 3)

Cut # ctrl + x content input box
elem.send_keys (Keys.CONTROL, 'X')
the time.sleep (. 3)

# re-enter the search input box
elem.send_keys (Keys.CONTROL, 'V')
the time.sleep (. 3 )

# replace by the Enter key clicks
driver.find_element_by_id ( "su"). send_keys (Keys.ENTER)
the time.sleep (3)

driver.quit ()

Guess you like

Origin www.cnblogs.com/zhichao123/p/11648116.html