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() options.add_argument('disable-infobars') driver = webdriver.Chrome(chrome_options=options) # driver = webdriver.Chrome() driver.get("http://www.jrj.com.cn/") sleep(2) source = driver.find_element_by_xpath(".//*[@id='appherw']") ActionChains(driver).move_to_element(source).perform()
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 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.
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() driver.get("http://www.baidu.com") # Input box input content driver.find_element_by_id("kw").send_keys("selenium") sleep(3) # Delete an m with multiple inputs driver.find_element_by_id("kw").send_keys(Keys.BACK_SPACE) sleep(3) # Continue entering tutorial driver.find_element_by_id("kw").send_keys(u"course") sleep(3) # ctrl+a select all input box content driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'a') sleep(3) # ctrl+x cut input box content driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'x') sleep(3) # Input box retype content, search driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'v') sleep(3) # Press enter instead of Click driver.find_element_by_id("su").send_keys(Keys.ENTER) sleep(3) driver.quit()
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.