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?
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?
Share Improve this question edited Sep 27, 2019 at 7:53 someuser2491 asked Sep 27, 2019 at 7:47 someuser2491someuser2491 1,9785 gold badges35 silver badges75 bronze badges 1-
This would work in a browser:
[...document.querySelectorAll("[class^=class]")].forEach(ele => ele.style.display = "none");
– mplungjan Commented Sep 27, 2019 at 7:51
2 Answers
Reset to default 5If 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";
}
}
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)