"Web Automation Test-code Five" common methods-Mouse keyboard events

Source: Internet
Author: User
Tags xpath

Java Python Ruby
Mouse events Right-click Actions action = new actions (driver);
Action.contextclick (InputBox). Build (). Perform ()
Actionchains (Driver). Context_click (Element). Perform () Driver.action.context_click (Element). Perform
Double click Action.doubleclick (Element). Build (). Perform () Actionchains (Driver). Double_click (Element). Perform () Driver.action.double_click (Element). Perform
Left click Action.clickandhold (Element). Build (). Perform () Actionchains (Driver). Click_and_hold (Element). Perform () Driver.action.click_and_hold (Element). Perform
Drag Action.draganddrop (source element, target Element). Build (). Perform (); Actionchains (Driver). Drag_and_drop (source element, target Element). Perform () Driver.action.drag_and_drop (source element, target element). Perform
Hover Action.movetoelement (driver.findelement (Element). Build (). Perform () Actionchains (Driver). Move_to_element (Element). Perform () Driver.action.move_to (Element). Perform
Keyboard events Fallback key (ESC) Action.sendkeys (element, Keys.escape). Perform () element. Send_keys (Keys.escape) element. Send_keys:escape
Keyboard F1 Action.sendkeys (element, KEYS.F1). Perform () element. Send_keys (KEYS.F1) element. send_keys:f1
Home key Action.sendkeys (element, Keys.home). Perform () element. Send_keys (Keys.home) element. Send_keys:home
End key Action.sendkeys (element, Keys.end). Perform () element. Send_keys (Keys.end) element. send_keys:end
Insert key Action.sendkeys (element, Keys.insert). Perform () element. Send_keys (Keys.insert) element. Send_keys:insert
Delete key Action.sendkeys (element, Keys.delete). Perform () element. Send_keys (Keys.delete) element. Send_keys:d elete
Delete key (Backspace) Action.sendkeys (element, Keys.back_space). Perform () element. Send_keys (Keys.back_space) element. Send_keys:backspace
TAB key (TAB) Action.sendkeys (element, Keys.tab). Perform () element. Send_keys (Keys.tab) element. Send_keys:tab
SPACEBAR (space) Action.sendkeys (element, Keys.space). Perform () element. Send_keys (Keys.space) element. Send_keys:space
Enter (enter) Action.sendkeys (element, Keys.enter). Perform () element. Send_keys (Keys.enter) element. Send_keys:enter
Shift key Action.sendkeys (element, Keys.shift). Perform () element. Send_keys (Keys.shift) element. Send_keys:shift
ALT key Action.sendkeys (element, Keys.alt). Perform () element. Send_keys (Keys.alt) element. Send_keys:alt
Page up key (PgUp) Action.sendkeys (element, keys.page_up). Perform () element. Send_keys (KEYS.PAGE_UP) element. Send_keys:p Age_up
Page down key (PgDn) Action.sendkeys (element, Keys.page_down). Perform () element. Send_keys (Keys.page_down) element. Send_keys:p Age_down
Up key (↑) Action.sendkeys (element, Keys.up). Perform () element. Send_keys (Keys.up) element. Send_keys:up
Next Key (↓) Action.sendkeys (element, Keys.down). Perform () element. Send_keys (Keys.down) element. Send_keys:d Own
Left (←) Action.sendkeys (element, Keys.left). Perform () element. Send_keys (Keys.left) element. Send_keys:enter
Right-click (→) Action.sendkeys (element, Keys.right). Perform () element. Send_keys (Keys.right) element. Send_keys:left
Select All (Ctrl + a) Driver.findelement (By.id ("kw")). SendKeys (Keys.chord (Keys.control, "a")) element. Send_keys (Keys.control, ' a ') element. Send_keys [: Control, ' a ']
Copy all selection (Ctrl + C) Driver.findelement (By.id ("kw")). SendKeys (Keys.chord (Keys.control, "C")) element. Send_keys (Keys.control, ' C ') element. Send_keys [: Control, ' C ']
Cut Select All (Ctrl+x) Driver.findelement (By.id ("kw")). SendKeys (Keys.chord (Keys.control, "X")) element. Send_keys (Keys.control, ' x ') element. Send_keys [: Control, ' X ']
Paste all selection (Ctrl + V) Driver.findelement (By.id ("kw")). SendKeys (Keys.chord (Keys.control, "V")) element. Send_keys (Keys.control, ' V ') element. Send_keys [: Control, ' V ']
Number 1 Action.sendkeys (element, Keys.numpad1). Perform () element. Send_keys (KEYS.NUMPAD1) element. Send_keys:numpad1
The corresponding package needs to be imported before calling part of the method
Java:
Import Org.openqa.selenium.interactions.Actions

Python:
From Selenium.webdriver.common.action_chains import Actionchains
From Selenium.webdriver.common.keys import keys

↓↓↓o (o ' ω′) ツ┏━┓ (knock table ~ ~ ~) Look down:
In Java, you need to refer to the package that you just mentioned in mouse events and keyboard key events. Why am I not using this method in the keyboard key combination event? Because I tried many times did not succeed (if the small partners know that the method of the combination of key usage, please leave a message ~), so in the keyboard combo key event adopted another method (the method does not need to import the actions package), the method is also applicable to keyboard single-button event.

* * Code Time * *

( ̄??) Hahahaha event A bit more, here to pick a few to run a code (interested can all experiment with their own (^u^) ノyo)

Java
1  Packagejavatest;2 3 Importorg.openqa.selenium.By;4 ImportOrg.openqa.selenium.Keys;5 ImportOrg.openqa.selenium.WebDriver;6 ImportOrg.openqa.selenium.firefox.FirefoxDriver;7 Importorg.openqa.selenium.interactions.Actions;8 9  Public classTest {Ten      Public Static voidMain (string[] arg)throwsinterruptedexception One     { AWebdriver Driver =Newfirefoxdriver (); -Driver.get ("http://www.baidu.com/"); -  theActions action =NewActions (driver); -Driver.findelement (By.id ("kw")). SendKeys ("JAVA1");//assign value to Baidu input box -Thread.Sleep (2000); -Action.sendkeys (Driver.findelement (by.id ("kw")), Keys.back_space). Perform ();//Delete key +Action.sendkeys (Driver.findelement (by.id ("kw")), Keys.enter). Perform ();//Enter -Thread.Sleep (2000); +Driver.findelement (By.id ("kw")). SendKeys (Keys.chord (Keys.control, "a"));//Copy AThread.Sleep (2000); atDriver.findelement (By.id ("kw")). SendKeys (Keys.chord (Keys.control, "X"));//Cut -Thread.Sleep (2000); -Driver.close ();//End -     } -}

Python
1  fromTimeImport*2  fromSeleniumImportWebdriver3  fromSelenium.webdriver.common.byImport by4  fromSelenium.webdriver.common.keysImportKeys5 6 #start the Firefox browser7Driver =Webdriver. Firefox ()8Driver.get ('http://www.baidu.com')9 TenDriver.find_element (By.xpath,"//*[@class = ' s_ipt ')"). Send_keys ('Python1')#assign value to Baidu input box OneSleep (2) ADriver.find_element (By.id,'kw'). Send_keys (Keys.back_space)#Delete key -Driver.find_element_by_xpath ("//input[@type = ' Submit ']"). Send_keys (Keys.enter)#Enter -Sleep (2)#Sleep 2 seconds theDriver.find_element (By.id,'kw'). Send_keys (Keys.control,'A')#Copy -Sleep (2)#Sleep 2 seconds -Driver.find_element (By.id,'kw'). Send_keys (Keys.control,'X')#Cut -Sleep (2) +  -Driver.close ()#End

Ruby
1 classBaidu2Require'RubyGems'3Require'Selenium-webdriver'4 5   #Open Firefox and enter the URL6Driver = Selenium::webdriver. for: Firefox7Driver.navigate.to"http://www.baidu.com"8 9Driver.find_element (: Name,'WD'). Send_keys ('Ruby1')#assign value to Baidu input boxTenSleep (2)#Sleep 2 seconds OneDriver.find_element (: ID,'kw'). Send_keys:backspace#Delete key ADriver.find_element (: ID,'kw'). Send_keys:enter#Enter -Sleep (2) -Driver.find_element (: XPath,"//*[@class = ' s_ipt ')"). Send_keys [: Control,'a']#Copy theSleep (2) -Driver.find_element (: ID,'kw'). Send_keys [: Control,'x']#Cut -Sleep (2) -  +Driver.close#Exit Program -End

"Web Automation Test-code Five" common methods-Mouse keyboard events

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.