The following code declares a HtmlUnitDriver
with javascript enabled using new HtmlUnitDriver(true)
and then creates a JavaScriptExecutor
like below
WebDriver driver = new HtmlUnitDriver(true);
JavascriptExecutor js = (JavascriptExecutor) ((HtmlUnitDriver)driver);
I find the html tag on which I have to add an attribute
String xpathSrc = "//*[@id='formId']/table/tbody/tr/td/table/tbody/tr[2]/td[2]/table/tbody/tr/td/table/tbody/tr[4]/td[2]/input[2]";
String xpathDest = "//*[@id='formId']/table/tbody/tr/td/table/tbody/tr[2]/td[2]/table/tbody/tr/td/table/tbody/tr[8]/td[2]/input[2]";
WebElement elemSrc = driver.findElement(By.xpath(xpathSrc));
setAttributeValue(elemSrc, "value", src.toUpperCase());
WebElement elemDest = driver.findElement(By.xpath(xpathDest));
setAttributeValue(elemDest, "value", dest.toUpperCase());
then I call the following function
public void setAttributeValue(WebElement elem, String attr, String value){
String scriptSetAttrValue = "arguments[0].setAttribute(arguments[1],arguments[2]);";
js.executeScript(scriptSetAttrValue, elem, attr, value);
}
But after all my efforts I am unable to execute the code with HtmlUnitDriver
but the same code runs efficiently with FirefoxDriver
or PhantomJsDriver
. I am inclined to use HtmlUnitDriver because of its speed but nothing seems to work.