Selenium (X): By using positioning elements, mouse events, keyboard events

1. By positioning element

In addition to the units previously described methods, the WebDriver also provides another set of writing, i.e. find_element unified call () method, to declare By positioning method, and positioning parameters corresponding to the incoming positioning method. details as follows:

from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By

wd = webdriver.Chrome()

wd.get('https://www.baidu.com/')

wd.find_element(By.ID,"kw")
wd.find_element(By.NAME,"wd")
wd.find_element(By.CLASS_NAME,"s_ipt")
wd.find_element(By.TAG_NAME,"input")
wd.find_element (By.LINK_TEXT, U " News " )
wd.find_element(By.PARTIAL_LINK_TEXT,u"")
wd.find_element(By.XPATH,"//*[@class='bg s_btn']")
wd.find_element(By.CSS_SELECTOR,"span.bg.s_btn_wr>input#su")

find_element () method is used only for locating elements. It requires two parameters, the first parameter is a type of positioning, by By: DETAILED second parameter is located. By introducing the required class By prior to use.

from selenium.webdriver.common.by import By

Implementation code is one thing they found that children, for example, to achieve find_element_by_id () method by looking at the bottom of WebDriver.

def find_element_by_id(self, id_):
    """Finds an element by id.

    :Args:
    - id\_ - The id of the element to be found.

    :Returns:
    - WebElement - the element if it was found

    :Raises:
    - NoSuchElementException - if the element wasn't found

    :Usage:
        element = driver.find_element_by_id('foo')
    """
    return self.find_element(by=By.ID, value=id_)

But WebDriver more recommended wording described earlier, of course, is the bottom of my company to use By dispensing function.

2. Mouse Events

Front only used click () method, but also understand the other mouse interaction, such as a mouse click, double click, hover, or even a mouse drag function.

In this chapter, I will explain these functions in detail.

In WebDriver, the mouse on these methods provide a packaging operation in ActionChains class.

ActionChains class provides a common method of operation of the mouse:

perform (): execution behavior of all stored ActionChains

context_click (): Right-click

double_click (): double-click

drag_and_drop (): Drag

move_to_element (): Hover

2.1 mouse right-click operation

Method for mouse usage classes provide ActionChains previously learned click () method is different.

I thought for a long time, Leng Shimo which sites can expect right, so we can not come up with an example of.

Introducing the mouse provided ActionChains:

from selenium.webdriver import ActionChains

Call ActionChains (driver) class, the browser driver driver as an argument, I usually use the name wd browser driven.

ActionChains(wd)

context_click (right) method for you right mouse seconds, the call is the need for positioning elements.

ActionChains(wd).context_click(right)

Perform all acts ActionChains stored, it can be understood to be submitted to the action of the entire operation.

ActionChains(wd).context_click(right).perform()

2.2 mouseover pop-up drop-down menu is a very functional design is created.

move_to_element () method can simulate mouse-over operation, and its usage context_click () the same.

from time import sleep
from selenium import webdriver
from selenium.webdriver import ActionChains

wd = webdriver.Chrome()

wd.get('https://www.baidu.com/')

# Targeting elements to hover 
above wd.find_element_by_xpath = ( " // div [@ ID = 'U1'] / A [. 8] " )
 # of positioning elements to perform mouse hovering operation 
ActionChains (wd). move_to_element (above) .perform ()

2.3 double click operation

double_click method for seconds you double click operation. And previous use exactly the same, do not code demonstrates.

2.4 drag and drop operation

drag_and_drop (source, target) holding down the left mouse button on the source element, and then moved to release the target element.

source: mouse to drag the source element

target: the mouse is released target element

This is also where I did not expect to be used, it may be sliding back a verification code is required.

3. Keyboard Events

Keys () method of class provides almost all of the keys on the keyboard. Front learned, send_keys () method can be used to simulate keyboard input, in addition, we can also use it to enter keys on the keyboard, or even a combination of keys, such as Ctrl + A, Ctrl + C and the like.

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

wd = webdriver.Chrome()

wd.get('https://www.baidu.com/')

# Input box contents 
wd.find_element_by_id ( " kw " ) .send_keys ( " seleniumm " )

# Delete a multi-input m of 
wd.find_element_by_id ( " kw " ) .send_keys (Keys.BACK_SPACE)

# Input Space + "Tutorial" 
wd.find_element_by_id ( " kw " ) .send_keys (Keys.SPACE)
wd.find_element_by_id("kw").send_keys("教程")

# The Ctrl + A select all content input box 
wd.find_element_by_id ( " kW " ) .send_keys (Keys.CONTROL, ' A ' )

# The Ctrl + X shear content input box 
wd.find_element_by_id ( " kW " ) .send_keys (Keys.CONTROL, ' X ' )

# The Ctrl + V to paste the contents of the input box 
wd.find_element_by_id ( " kW " ) .send_keys (Keys.CONTROL, ' V ' )

# To replace by clicking the Enter key Operation 
wd.find_element_by_id ( " su " ) .send_keys (Keys.ENTER)

It should be noted that the above code is of little practical significance, only to show us all kinds of simulations using the keyboard keys and key combinations.

Prior to use keyboard keys class method needs to be imported.

from selenium.webdriver.common.keys import Keys

The following is a commonly used keyboard:

send_keys (Keys.BACK_SPACE) Delete key

send_keys (Keys.SPACE) Spacebar

send_keys (Keys.TAB) Tabulator

send_keys (Keys.ESCAPE) Backspace key

send_keys (Keys.ENTER) Enter key

send_keys(Keys.CONTROL,'a')  全选

send_keys (Keys.CONTROL, 'c') Copy

send_keys (Keys.CONTROL, 'X' ) Shear

send_keys (Keys.CONTROL, 'V' ) Paste

send_keys (Keys.F1) Keyboard F1

......

send_keys (Keys.F12) keyboard F12

Guess you like

Origin www.cnblogs.com/liuhui0308/p/11960688.html