Mouse and keyboard operation events

Mouse events

Mouse events are included in the ActionChains class. When importing, you only need to:

from selenium.webdriver.common.action_chains import ActionChains

Import the class to:

Common methods are:

  • context_click() -- right click

  • double_click() -- double click

  • drag_ and_ Drop - drag

  • move_to_element() -- mouse over an element

  • click_and_hold() -- press the left mouse button on an element

It should be noted that these actions on the mouse in ActionChains class can only be executed with perform.

Here's a piece of code. Let's see the effect:

# coding: utf-8
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChainsoptions = webdriver.ChromeOptions()
driver = webdriver.Chrome(chrome_options=options)
# driver = webdriver.Chrome()
source = driver.find_element_by_xpath(".//*[@id='appherw']")

Well, I have to say, I stepped on the pit You have also found that I use Chrome browser, yes, because Firefox just can't drag I Baidu a circle did not find the reason. I can only tell you about my version of chrome; besides, if you don't write like me but directly webdriver.Chrom(), it's OK to run, but when Chrome is turned on, it will display a line of words: "Chrome is under the control of automatic test software". With chrome, you have to install the chrome driver.

After running the above code, you will find that the QR code is shown below - this is exactly the response when the mouse moves up.
Remind you again, the operation of the mouse should have. perform().

Keyboard events

Keyboard events are some operations on the keyboard, such as Ctrl +C, Ctrl+V, Ctrl+X, etc.
Operations on the keyboard need to import another keyboard's Library:

from selenium.webdriver.common.keys import Keys

For example, you need to type "automatic test" in the search box, but now you want to search for "automatic test", that is, delete a word, we know, just press the Backspace key on the keyboard, and then you need keyboard operation:

driver.find_element_by_xpath("xpath Positioning of").send_keys(Keys.BACK_SPACE)

If for software testing, interface testing, automation testing, interview experience exchange. If you are interested, you can add software test exchange: 1085991341, and there will be technical exchanges with peers.
That's it.
Next, let's remember the code:

# coding: utf-8
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
# Input box input content
# Delete an m with multiple inputs
# Continue entering tutorial
# ctrl+a select all input box content
# ctrl+x cut input box content
# Input box retype content, search
# Press enter instead of Click

It's almost enough to understand the above content. If we meet new needs, we only need Baidu for a while. We need to know that we can't remember everything in our mind. As long as we get started and know how Baidu works, we can achieve the goal. Of course, it's better to remember.
The above content hopes to be helpful to you. Friends who have been helped are welcome to like and comment.

Keywords: Programming Selenium Firefox

Added by snipesh0tz on Mon, 01 Jun 2020 04:12:15 +0300