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);