Selenium2+python automation 12-manipulating elements (keyboard and mouse events)

foreword

在前面的几篇中重点介绍了一些元素的到位方法,到位到元素后,接下来就是需要操作元素了。This article summarizes some commonly used methods of operating elements on web pages, which can be collectively referred to as behavior events
有些web界面的选项菜单需要鼠标悬停在某个元素上才能显示出来(如百度页面的设置按钮)。

1. Simple operation

    1. Click (left mouse button) the page button: click()

    2. Please empty the input box: clear()

    3. Input string: send_keys()

    4. After opening the test tribe forum, click the magnifying glass search icon. Generally, in order to ensure the correctness of the input, you can first clear the input box, and then enter the search keyword

 

Second, submit the form

    1. In the previous Baidu search case, after entering the keyword, you can directly press the Enter key to search, or you can click the search button to search.

    2.submit() is generally used to simulate the enter key

    3. But for forum search, if you use submit, an error will be reported, but there is no search click button, what should I do?


3. Keyboard operation

    1. Selenium provides a complete set of simulated keyboard operation events. If the previous submit() method does not work, you can try to simulate keyboard events

    2. The operation of simulating the keyboard needs to import the keyboard module first: from selenium.webdriver.common.keys import Keys

    3. To simulate the enter key, you can use send_keys(Keys.ENTER)

    4. Other common keyboard operations:

Keyboard F1 to F12: send_keys(Keys.F1) Change F1 to the corresponding shortcut key

Copy Ctrl+C: send_keys(Keys.CONTROL,'c') 

Paste Ctrl+V: send_keys(Keys.CONTROL,'v') 

 全选Ctrl+A:send_keys(Keys.CONTROL,'a') 

剪切Ctrl+X:send_keys(Keys.CONTROL,'x') 

Tab keys Tab: send_keys(Keys.TAB) 

Here are just some commonly used ones. Of course, in addition to keyboard events, there are also mouse events.


Fourth, the mouseover event

    1. The mouse can not only click (click), the mouse has other operations, such as: hovering the mouse over an element, right-clicking the mouse, holding down a button and dragging the mouse to the

    2. Mouse events need to be imported first: from selenium.webdriver.common.action_chains import ActionChains

perform() executes the actions in all ActionChains

move_to_element() mouseover

    3. Here is an example of Baidu page setting button

    4. In addition to the commonly used mouseover events, there are

Right mouse click: context_click()

Double-click the mouse: double_click()

Just follow the gourd and draw the scoop and replace the corresponding mouse event in the above case.

Selenium provides a complete set of mouse and keyboard behavior events, which are quite powerful. The next article introduces how to deal with multiple windows

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325342905&siteId=291194637