Java 关注WebElement?

Java 关注WebElement?,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我目前正在测试我的应用程序的GUI,我想知道是否可以将焦点设置为WebElement 我正在使用Selenium 2.0和webdriver 所以我在寻找这样的东西:driver.findElementxxxx.setfocus 多谢各位 编辑: 我已经试过那种棘手的事情了 // getting the element WebElement eSupplierSuggest = driver.findElement(By.xpath("...")); //get le location an

我目前正在测试我的应用程序的GUI,我想知道是否可以将焦点设置为WebElement

我正在使用Selenium 2.0和webdriver

所以我在寻找这样的东西:driver.findElementxxxx.setfocus

多谢各位

编辑: 我已经试过那种棘手的事情了

// getting the element WebElement 
eSupplierSuggest = driver.findElement(By.xpath("..."));

//get le location and click 
Point location = eSupplierSuggest.getLocation();
new Actions(driver).moveToElement(eSupplierSuggest, location.x, location.y).click();

//or

//directly perform
new Actions(driver).moveToElement(eSupplierSuggest).click().perform();
我在某个地方用红色表示单击会聚焦元素,但在我的情况下,没有任何效果。哈哈


PS:这是原始帖子的一个子问题

请尝试使用cssSelector进行如下所示的自动建议单击,并告知我您是否仍面临此问题

 // supplier ops, i find and type data into the input
    WebElement eSupplier = driver.findElement(By.id("supplier:supplierOps_input"));
    eSupplier.sendKeys("OPS1");
    sleep(5); // wait the suggestbox

    // i find the suggestbox
    WebElement eSupplierSuggest = driver.findElement(By.cssSelector("css path of that specific value in the auto suggestion box"));
    eSupplierSuggest.click();
    sleep(5); // wait the refresh for the next field supplierAddress

尝试使用cssSelector进行自动建议单击,如下所示,如果您仍然面临此问题,请告诉我

 // supplier ops, i find and type data into the input
    WebElement eSupplier = driver.findElement(By.id("supplier:supplierOps_input"));
    eSupplier.sendKeys("OPS1");
    sleep(5); // wait the suggestbox

    // i find the suggestbox
    WebElement eSupplierSuggest = driver.findElement(By.cssSelector("css path of that specific value in the auto suggestion box"));
    eSupplierSuggest.click();
    sleep(5); // wait the refresh for the next field supplierAddress

WebDriver API中没有设置元素焦点的函数


如果您想这样做,您必须编写一些JavaScript来设置焦点,然后使用JavaScriptExecutor来运行JavaScript。

WebDriver API中没有在元素上设置焦点的函数


如果您想这样做,您必须编写一些JavaScript来设置焦点,然后使用JavaScriptExecutor来运行JavaScript。

确保您没有更改帧。。。。
另一方面。单击应执行此操作

确保您没有更改帧。。。。
另一方面,单击应该可以做到这一点

我通常会向元素发送一个空键,以便它聚焦。例如:

element.send_keys ""

我通常会向元素发送一个空键,以便它聚焦。例如:

element.send_keys ""

要在元素上设置焦点,可以使用executeScript方法,如下所述:

JavascriptExecutor js;
js.executeScript ("document.getElementById('x').focus()");

设置焦点后,您可以轻松使用webdriver api提供的send_键。

为了在元素上设置焦点,您可以使用executeScript方法,如下所述:

JavascriptExecutor js;
js.executeScript ("document.getElementById('x').focus()");

一旦设置了焦点,您就可以轻松地使用webdriver api提供的send_键。

使用任何框架?或者只是html css jsapplication@daemon:是的,我正在使用Selenium 2,我会在问题上添加它。我的错,你能告诉我你为什么需要专注吗?它是否与您正在测试的自动建议框类似?@HemChe,正如我在PS中所说的。我想在建议框中选择一个选项,然后单击它进行验证。我可以找到元素和选项,但当我单击它时,什么也没有发生,所以我想使用其他方法,例如,设置焦点和sendkeykeys.ENTER。另请参见:使用任何框架?或者只是html css jsapplication@daemon:是的,我正在使用Selenium 2,我会在问题上添加它。我的错,你能告诉我你为什么需要专注吗?它是否与您正在测试的自动建议框类似?@HemChe,正如我在PS中所说的。我想在建议框中选择一个选项,然后单击它进行验证。我可以找到元素和选项,但当我单击它时,什么也没有发生,因此我想使用其他方法,如设置焦点和sendkeykeys.ENTER。另请参见: