selenium webdriver類比滑鼠鍵盤操作

來源:互聯網
上載者:User

標籤:滑鼠右鍵   alt   context   指定   xpath   .com   快捷   space   send   

在測試使用Selenium webdriver測試WEB系統的時候,用到了類比滑鼠、鍵盤的一些輸入操作。

1、滑鼠的左鍵點擊、雙擊、拖拽、右鍵點擊等;

2、鍵盤的斷行符號、回退、空格、ctrl、alt、shift等;

在webdriver中,有專門的一個類,是用來進行滑鼠、鍵盤類比操作的,那就是Actions類,該類使用時,又會涉及到Keyboard、Mouse、CompositeAction(複合動作),先對Mouse的方法做簡單羅列,然後再用代碼說明:

1、滑鼠左鍵點擊:

Actions action = new Actions(driver);

action.click(driver.findElement(By.by);

左鍵點擊是普通常用的方法,前面已經介紹過一種更簡單的方法:

Driverdriver=new Driver();

driver.findElement(By.xpath(xpath)).click();

 

2、滑鼠左鍵雙擊:

Actionsaction = new Actions(driver);

action.doubleClick(driver.findElement(By.xpath(xpath)));

 

3、滑鼠左鍵按下操作:

Actionsaction = new Actions(driver);

action.clickAndHold(driver.findElemen(By.xpath(xpath)));

 

4、滑鼠左鍵移動到元素操作:

Actionsaction = new Actions(driver);

action.moveToElement(driver.findElement(By.xpath(xpath)));

 

5、滑鼠右鍵點擊操作:

Actionsaction = new Actions(driver) ;

action.contextClick(driver.findElement(By.xpath(xpath)));

 

6、組合的滑鼠操作(將目標元素拖拽到指定的元素上):

Actionsaction = new Actions(driver);

action.dragAndDrop(driver.findElement(By.xpath(xpath)),driver.findElement(By.xpath(xpath)));

 

7、組合的滑鼠操作(將目標元素拖拽到指定的元素上):

Actionsaction = new Actions(driver);

action.dragAndDrop(driver.findElement(By.xpath(xpath)),xOffset,yOffset);

 

代碼示範:

driver.get("http://www.baidu.com");

driver.findElement(By.name("wd")).sendKeys("林丹");

 

//點擊操作

driver.findElement(by.id("su")).click();

driver.navigate().back();

Actionsaction=new Actions(driver);

action.click(driver.findElement(by.id("su")));

 

//雙擊操作(略)

 

//拖拽選擇操作,滑鼠按住不放,進行拖拽選擇,然後釋放滑鼠,需要三個動作

action.clickAndHold(driver.findElement(By.id("1"))).moveToElement(driver.findElement(By.id("3"))).perform();

action.release();

 

//拖拽元素,將一個元素拖拽到另一個元素上,然後釋放滑鼠(略)

 

鍵盤類比操作,包括普通按鍵,比如enter、backspace、tab等,還包括四個修飾鍵(Modifier Keys),分別是Caps Lock,Control,Option,Command。

普通按鍵使用時,直接使用sendkeys(theKeys)就可以,如按下enter鍵:

action.sendKeys(Keys.ENTER).perform();

 

修飾鍵一般使用時,是和別的按鍵配合使用的,比如快速鍵ctrl+F4,這時就得使用keyDown(theKeys)、keyUp(theKeys)來操作,代碼如下:

driver.get("http://www.baidu.com");

driver.findElement(by.name("wd")).sendKeys("林丹");

Actionsaction=new Actions(driver);

//輸入框中輸入內容,然後點擊斷行符號進行百度

action.sendKeys(Keys.ENTER).perform();

//使用快速鍵alt+f4關閉視窗(但是該方法不穩定,時行時不行,不行居多)

action.keyDown(keys.ATL).keyDown(keys.F4).keyUp(keys.ALT).perform();

//使用ctrl+a全選

driver.findElement(By.id("kw")).click();

action.sendKeys(Keys.CONTROL+"a").perform();

 

Tips:

1、拖拽選擇時,使用clickAndHold和moveToElement時,會受到滑鼠所在位置的影響,所以盡量是兩個方法一起使用;

2、拖拽元素時,dragAndDrop方法,我沒有找到實現的情境,所以沒有寫代碼實驗;

3、直接按下普通按鍵時,直接使用sendkeys(theKeys)方法就能夠實現;

4、需要使用修飾鍵時,需要連貫動作中應該使用keydown和keyup方法;

action.keyDown(keys.ATL).sendKeys(keys.F4).keyUp(keys.ALT).perform();//這是不能實現alt+f4關閉視窗的效果的,只能實現單獨按f4的效果

5、對於一些快速鍵,如ctrl+a,可以直接sendkeys(Keys.CONTROL+"a")。

selenium webdriver類比滑鼠鍵盤操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.