如何访问隐形元素

时间:2016-07-25 09:06:06

标签: selenium-webdriver invisible

  1. 将鼠标悬停在元素

  2. 一旦我们悬停,就会有

    • 文本框
    • 点击提交按钮
  3. 首先,我必须使用鼠标hover,然后在Textbox中输入值,然后点击Submit按钮。

2 个答案:

答案 0 :(得分:1)

请参阅以下代码以执行所需操作。

// Initializing the action class
Actions action = new Actions(driver);

// Moving to the element
action.moveToElement(<WebElement>).build().perform();

// Entering the text in the text box
action.moveToElement(<WebElement of Textbox>).sendKeys("Text").build().perform();

// Clicking on the submit button
action.moveToElement(<WebElement of submit button>).click().build().perform();

或者您可以将上述所有操作合并为单一操作。

Actions action = new Actions(driver);
action.moveToElement(<Element which displayes text box>).moveToElement(<Element of textbox>).sendKeys("Text").moveToElement(<Element of submit button>).click().build().perform();

希望这有帮助。

答案 1 :(得分:0)

(有趣的是你正在使用java)根据你的方法,你应该尝试使用Mouse在元素上执行鼠标悬停然后使用WebDriverWait查找元素,等待元素可见,如下所示: - < / p>

import org.openqa.selenium.interactions.HasInputDevices
import org.openqa.selenium.interactions.Mouse
import org.openqa.selenium.internal.Locatable;

WebDriverWait wait = new WebDriverWait(driver, 10);

//Find element first where you want to hover
WebElement hoverElement = wait.until(ExpectedConditions.visibilityOfElementLocated(byObject));
Mouse mouse = ((HasInputDevices)driver).getMouse();
mouse.mouseMove(((Locatable)hoverElement).getCoordinates()); //it will perform mouse over on desire element     

//Now after mouse over you can find the desire text box
WebElement textBox = wait.until(ExpectedConditions.visibilityOfElementLocated(byObject));
users.sendKeys("your value"); //It will set the value on text box

//Now you can find the desire submit button
WebElement submit = wait.until(ExpectedConditions.elementToBeClickable(byObject));
submit.click(); //It will click on submit button

已修改: - 如果鼠标悬停无法使用Mouse,则您可以使用JavascriptExecutor执行鼠标悬停,如下所示: -

JavascriptExecutor js = (JavascriptExecutor)driver;
executor.executeScript("function triggerMouseEvent (node, eventType) {"
                    + "var clickEvent = document.createEvent ('MouseEvents');"
                    + "clickEvent.initEvent (eventType, true, true);"
                    + "node.dispatchEvent (clickEvent);"
                    + "}triggerMouseEvent (arguments[0], 'mouseover');", hoverElement);

希望它有所帮助.. :)