Mouse and keyboard operation events

Mouse event

Mouse events are contained in the ActionChains class, and only need to be imported:

from selenium.webdriver.common.action_chains import ActionChains

Import the class:

The commonly used methods are:

  • context_click()-right click

  • double_click()-double click

  • drag_and_drop (the element pressed by the mouse, the element released by the mouse)-drag

  • move_to_element()-hover over an element

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

It should be noted that the operations on the mouse in these ActionChains classes can be executed only when perform is added.

Let’s take a look at 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()

Uh, I have to say, I stepped on a pit... Everyone found out that I am using the Chrome browser, yes, because Firefox just can't drag... I didn't find the reason after a lot of Baidu. I can only tell you about my chrome version; in addition, if you don’t write like I did but directly webdriver.Chrom(), the operation is fine, but when chrome is opened, it will display a line-"chrome is being automatically Test software control". If you use chrome, you have to install chromedriver.


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

Keyboard events

Keyboard events are some operations on the keyboard, such as Ctrl+C, Ctrl+V, Ctrl+X, etc.
The operation of the keyboard needs to import another keyboard library:

from selenium.webdriver.common.keys import Keys

For example, if you want to enter "automated test" in the search box, but now you want to search for "automated test", you just delete a word. We know that just press the Backspace key on the keyboard. Need keyboard operation:

driver.find_element_by_xpath("xpath的定位").send_keys(Keys.BACK_SPACE)

If you exchange experience in software testing, interface testing, automated testing, and interviews. If you are interested, you can add software test communication: 1085991341, and there will be technical exchanges with colleagues.
that's it.
Next, come to a piece of code, everyone will remember:

# 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")
# 输入框输入内容
driver.find_element_by_id("kw").send_keys("selenium")
sleep(3)
# 删除多输入的一个 m
driver.find_element_by_id("kw").send_keys(Keys.BACK_SPACE)
sleep(3)
# 继续输入“教程”
driver.find_element_by_id("kw").send_keys(u"教程")
sleep(3)
# ctrl+a 全选输入框内容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'a')
sleep(3)
# ctrl+x 剪切输入框内容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'x')
sleep(3)
# 输入框重新输入内容,搜索
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'v')
sleep(3)
# 通过回车键盘来代替点击操作
driver.find_element_by_id("su").send_keys(Keys.ENTER)
sleep(3)
driver.quit()

It's almost enough to understand the above content. If you encounter new needs, you only need Baidu. We must know that we can't keep everything in our minds. As long as we get started and know how to Baidu, the goal will be achieved. Of course It's better if you remember.
The above content hopes to be helpful to you. Friends who have been helped are welcome to like and comment.

Guess you like

Origin blog.csdn.net/Chaqian/article/details/106455592