Webdriver中 focus on某个元素

昨天的注册中,验证码获取了,但是总是点注册的时候,似乎点了,但账号没有创建。应该是没有点中那个注册按钮。

回家的时候,思考了下,如果我先mouse over, 或者focus on到那个元素上,岂不就可以了么?

早上的时候,在谷歌上搜webdriver focus on,结果还真搜到了。

the following code -

element.sendKeys("");

tries to find an input tag box toenter some information, while

newActions(driver).moveToElement(element).perform();

is more appropriate as it will workfor image elements, link elements, dropdown boxes etc.

Therefore using moveToElement() method makes more sense to focus on any genericWebElement on the web page.

For an input box you will haveto click() on the element to focus.

newActions(driver).moveToElement(element).click().perform();

while for links and images themouse will be over that particular element,you can decide to click() on itdepending on what you want to do.

If the click() onan input tag does not work -

Since you want this function to begeneric, you first check if the webElement is an input tag or not by -

if("input".equals(element.getTagName()){
  
element.sendKeys("");
}
else{
  
newActions(driver).moveToElement(element).perform();

}

You can make similar changes basedon your preferences.

 

源文档 <http://stackoverflow.com/questions/11337353/correct-way-to-focus-an-element-in-selenium-webdriver-using-java>

 

然后我尝试了一下

public ZhucePageregistnow() {

System.out.println("hahaa");

newActions(driver).moveToElement(zcbutton).click().perform();

zcbutton.click();

System.out.println("hahaa1");

WebDriverWrapper.waitPageLoad(driver,3);

returnPageFactory.initElements(this.getDriver(), ZhucePage.class);

}

结果还真可以!

 

后面还看到有另外一种方法:

You can use JS as below:

WebDriver driver = newFirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript(
"document.getElementById('elementid').focus();");

 

源文档 <http://stackoverflow.com/questions/11337353/correct-way-to-focus-an-element-in-selenium-webdriver-using-java>

 

不过我没有尝试。

你可能感兴趣的:(Webdriver中 focus on某个元素)