修改元素的style,主要是将一些隐性元素显示出来,让元素可被操作:
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.getElementById(‘name‘).style.display=‘block‘;");
或
JavascriptExecutor js = (JavascriptExecutor)driver;
WebElement element = driver.findElement(By.xxx);
js.executeScript("arguments[0].style=arguments[1]",element,"display: block;");
对于某些富文本或者一些特殊类型的input输入框,使用javascript给其赋值:
File file = new File("resources/text");
WebElement input =driver.findElement(By.id("xxxx");((JavascriptExecutor)driver).executeScript("arguments[0].value=arguments[1]",input, file.getAbsolutePath());