Java

清除 Web 元素的内容:(注意 - 在测试中模拟用户操作时,最好发送退格,请参阅下一步操作)

interactionWebElement.clear();

输入数据 - 模拟发送击键:

interactionWebElement.sendKeys("Text");
interactionWebElement.sendKeys(Keys.CONTROL + "c"); // copy to clipboard.

获取元素属性的值:

interactionWebElement.getAttribute("value");
interactionWebElement.getAttribute("style");

获取元素的文本:

String elementsText = interactionWebElement.getText();

从下拉列表中选择:

Select dropDown = new Select(webElement);
dropDown.selectByValue(value);

自我解释:

interactionWebElement.click();
interactionWebElement.submit(); //for forms
interactionWebElement.isDisplayed();
interactionWebElement.isEnabled(); // for exampale - is clickable.
interactionWebElement.isSelected(); // for radio buttons.

使用 org.openqa.selenium.interactions.Actions行动

拖放:

Action dragAndDrop = builder.clickAndHold(someElement)
   .moveToElement(otherElement)
   .release(otherElement)
   .build();

dragAndDrop.perform();

选择多个:

Action selectMultiple = builder.keyDown(Keys.CONTROL)
   .click(someElement)
   .click(someOtherElement)
   .keyUp(Keys.CONTROL);

dragAndDrop.perform();

自解释(使用构建器):

builder.doubleClick(webElement).perform();
builder.moveToElement(webElement).perform(); //hovering

有关高级操作的更多示例和完整列表,请参见此处

使用 Javascript:

// Scroll to view element:
((JavascriptExecutor) driver).executeJavaScript("arguments[0].scrollIntoView(true);", webElement);