Home > Back-end >  How to find all elements that contains a value in a specific attribute using selenium python
How to find all elements that contains a value in a specific attribute using selenium python

Time:09-17

Hi am trying to find all elements that have display: none in its style attribute. For example in this div:

<div class="w3-col w3-border" id="imgbox1" style="width: 30%; display: none;"><p style="word-wrap: break-word;"><code class="w3-codespan" style="wrap">display:none</code></p>
  <img src="img_5terre.jpg" style="width:100%" alt="Italy">
  <p><button class="ws-btn w3-block" onclick="removeElement()">Remove</button></p>
</div>

How can I achieve this?

CodePudding user response:

The below xpath

//*[contains(@style,'display: none')]

should represent all the node which are having style as display none.

for this specific div, you can have //* replaced with //div. Something like this :

//div[contains(@style,'display: none')]

sample code :

disp_none = driver.find_element_by_xpath("//*[contains(@style,'display: none')]")

the above list disp_none should contains all the web elements.

Print the size like this :

print(len(disp_none))
  • Related