3

I want to hide elements on canvas element by executing a javascript in python.

I have tried below:

    def hide_elements():
        driver = LiveLibrary.get_webdriver_instance()

        js_script = '''\
        element1 = document.getElementsByClassName('someclass');
        element1[0].style.display = 'none';
        element2 = document.getElementsByClassName('another');
        element2[0].style.display = 'none';
        element3 = document.getElementsByClassName('highlight');
        element3[0].style.display = 'none';
        element4 = document.getElementsByClassName('overlay');
        element4[0].style.display = 'none';
        '''
        driver.execute_script(js_script)

The above works but as you can see there is a repetition of code. Is there a way I can simplify this rather than finding each element and hiding them?

1
  • This would work in a browser: [...document.querySelectorAll("[class^=class]")].forEach(ele => ele.style.display = "none"); Commented Sep 27, 2019 at 7:51

2 Answers 2

5

If only one of each class - I use ES<5 to not break your webdriver:

var cls = ["someclass","another","highlight","overlay"];
for (var i=0;i<cls.length;i++) {
  document.querySelector("."+cls[i]).style.display = "none";
}

This would work in a browser:

[...document.querySelectorAll("[class^=class]")].forEach(ele => ele.style.display = "none");

For different classes:

["someclass","another","highlight","overlay"]
  .forEach(cls => [...document.querySelectorAll("."+cls)]
  .forEach(ele => ele.style.display = "none"));

For older JS versions:

["someclass","another","highlight","overlay"]
  .forEach(function(cls) { [...document.querySelectorAll("."+cls)]
  .forEach(function(ele) { ele.style.display = "none"})});

Even older:

var cls= ["someclass","another","highlight","overlay"];
for (var i=0;i<cls.length;i++) {
  var elements = document.querySelectorAll("."+cls[i]);
  for (var j=0;j<elements.length;j++) {
    elements[j].style.display = "none";
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Why added answer when you prefer to comment and help OP
i get this error webdriverexception: SyntaxError: Unexpected token '=>'
Yeah, I would expect the version of JS would not allow that. see update
0

use following code :

js_script = '''\
document.getElementsByClassName('someclass')[0].setAttribute("hidden","");
document.getElementsByClassName('another')[0].setAttribute("hidden","");
document.getElementsByClassName('highlight')[0].setAttribute("hidden","");
document.getElementsByClassName('overlay')[0].setAttribute("hidden","");
 '''
driver.execute_script(js_script)

4 Comments

More or less same repetition, no?
@mplungjan : I am just trying to answer the question ..as he specifically mentioned the scripts to be executed..
@someuser2491 : could you please have a try at this
There is very little difference between original question and this answer. Instead of display, you set hidden and you save a line by adding the [0] to the getElementsByClassName. Not sure what is gained here DRY-wise

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.