Home > Back-end >  python selenium - locate elements which contains two styles inside style attribute
python selenium - locate elements which contains two styles inside style attribute

Time:10-26

Trying to locate all elements on page

<i data-visualcompletion="css-img"  style="background-image: url(&quot;https://static.xx.fbcdn.net/rsrc.php/v3/yd/r/A6Wu9TeIieI.png&quot;); background-position: 0px -1304px; background-size: auto; width: 16px; height: 16px; background-repeat: no-repeat; display: inline-block;"></i>

Since attributes inside style such as background-position: 0px -1304px; and ;background-image: url(&quot;https://static.xx.fbcdn.net/rsrc.php/v3/yd/r/A6Wu9TeIieI.png changes very often i can't locate by style like this

style ='background-image: url("https://static.xx.fbcdn.net/rsrc.php/v3/yd/r/A6Wu9TeIieI.png"); background-position: 0px -1304px; background-size: auto; width: 16px; height: 16px; background-repeat: no-repeat; display: inline-block;'
driver.find_elements(By.CSS_SELECTOR, f"i[style='{style}']")

What I'm looking for is some kind of contains including and.

I.E

 driver.find_elements(By.XPATH, f"//*[contains(@style, 
'width: 16px; height: 16px; background-repeat: no-repeat; display: inline-block;')]") 

AND Style Contains myOtherStyleHere

This is Facebook Marketplace three dots button

enter image description here

CodePudding user response:

You can use logical and operator inside the XPath locator, as following:

driver.find_elements(By.XPATH, "//*[contains(@style, 'width: 16px') and(contains(@style, 'height: 16px')) and(contains(@style, 'background-repeat: no-repeat')) and(contains(@style, 'display: inline-block'))]")

The above is example only. The idea is: you can use several contains conditions while each of them contains some desired fixed condition.

  • Related