1、 对input执行输入
直接设置value属性, 此方法主要应对输入框自动补全以及readonly属性的element,sendkeys不稳定
比如:
//inputbox is a WebElement
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].value="北京"", from_inpox);
对此可以封装一个typeQuick的方法
/**
* @author Young
* @param locator
* @param values
* @throws Exception
*/
protected void typeQuick(Locator locator, String values) throws Exception {
WebElement e = findElement(driver, locator);
log.info("type value is: " + values);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].value=""+values+""", e);
去掉只读属性
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].removeAttribute("+"readonly"+")", e);
2.对富文本框的操作
主要应对富文本框,可以封装获取富文本框内容和设置富文本路况内容的方法
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement editor = driver.findElement(By.tagName("body"));
js.executeScript(
"arguments[0].innerHTML = '<h1>Selenium Test </h1>I love Selenium <br> this article Post By Selenium WebDriver<br><h2>Create By Young</h2>'",
editor);
设置富文本框内容
/**
* @author Young
* @param locator
* @param text
*/
protected void setRichTextBox(Locator locator, String text) {
WebElement e = findElement(driver, locator);
log.info("type value is: " + text);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].innerHTML = "" + text + """, e);
}
获取富文本框内容:
/**
* @author Young
* @param locator
* @param text
* @return
*/
protected String getRichTextBox(Locator locator, String text) {
WebElement e = findElement(driver, locator);
log.info("type value is: " + text);
JavascriptExecutor js = (JavascriptExecutor) driver;
String result=(String) js.executeScript("arguments[0].getInnerHTML()", e);
return result;
}