How to use javascript to set attribute of selected web element using selenium Webdriver using java?

0
0

I want to use javascript to set attribute for selected element on webpage.

I have found 2 ways to set attribute using javascript

1

   WebDriver driver; // Assigned elsewhere
JavascriptExecutor js = (JavascriptExecutor) driver;
js
.executeScript("document.getElementByID('//id of element').setAttribute('attr', '10')");

2

WebElement element = driver.findElement(By.id("foo"));
String contents = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML;", element);

But I want to apply javascript to specific webelement which i have found using selenium webdriver

as an example i have select one link using selenium webdriver

driver.findElement(By.linkText("Click ME"))

Now I want to set attribute of this webelement using javascript

but I don’t know how to combine both

please help me to find solution

  • You must to post comments
0
0

Along the lines of:

JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.linkText("Click ME"));
js
.executeScript("arguments[0].setAttribute('attr', '10')",element);
  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website