0

I am trying to set an attribute value for all same kind of <img> tag in My website e.g

<img src="images/temp/advertisement.png">

and i wanted to set style="display:none" so I will be able to hide them.

I have tried following method -

List<WebElement> element = driver.findElements(By.tagName("img"));

    for(WebElement e:element)
    {

        if(e.getAttribute(src).contains("images/temp/advertisement.png"))
        {
            jse.executeScript("document."+e+".setAttribute('style', 'display: none;')");
        }
 }

but getting an error

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Runtime.evaluate threw exception: SyntaxError: Unexpected token [

Is any one help what is wrong here or what other I can do?

3 Answers 3

1

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Runtime.evaluate threw exception: SyntaxError: Unexpected token [

You are using JavascriptExecutor to execute javascript on an element but syntactically incorrect, in executeScript arguments will be made available to the JavaScript via the arguments magic variable, as if the function were called via Function.apply where arguments must be a number, a boolean, a String, WebElement etc.

You can try as below :-

List<WebElement> element = driver.findElements(By.tagName("img"));

for(WebElement e:element) {    
  if(e.getAttribute("src").contains("images/temp/advertisement.png")){
       jse.executeScript("arguments[0].style.display = 'none'", e);
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

it is not showing error now, but not able to set style="display:none" attribute.
Is it so you will be able to change the attribute value if that attribute present in html for that tag itselt?
@NarendraRajput Yes, you can update attribute value if it is present already at runtime using this..
Thanks . Its working . there was timing issue to load the images now its fine
1

There is problem with that you pass e like a object and there is called toString(), so final result is with [] etc... Another way could be something like this:

jse.executeScript(
        "var imgs = document.getElementsByTagName('img');" +
        "for(var i = 0; i < imgs.length; i++) { " +
        "    if (imgs[i].getAttribute('src').indexOf('images/temp/advertisement.png') != -1) { " +
        "       imgs[i].setAttribute('style', 'display: none;');" +
        "    }" +
        "}" );

I wrote it without test, so maybe you should edit it ;)

Comments

0

You use document to locate the WebElement. In your case you already located it. Try

jse.executeScript("arguments[0].setAttribute('style', 'display: none;')", e);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.