Press element with condition that has different parents and children in selenium

Viewed 51

I'm trying to press an element <a class="clean" href="url"> with python selenium on the condition that some elements have a certain value. Those elements are <div data-passendheid="Correct"> with value 'Correct', <path stroke-width="11"> with value '11' and span in <span class="js-tooltip"> that contains text 'Available'. I tried it with xpath: driver.find_elements(By.XPATH, "//*[@id='js-top']/div/div/a and [contains(.,'Available')]") But it doesn't work because the elements are from different parents and children. If you know how to do it with xpath or a have different solution please let me know.

I can't share the website because you need to login, instead see the corresponding page source:

<div class="box  box--obj  push-left  js-flow-block" data-passendheid="Correct">
  
     <div class="js-animate-fadein" style="">

        <a class="clean" href="url">

            <div class="box__properties  group">      
                      
                <div class="js-tooltip">
                    <div class="timer  js-tooltip__trigger id="js-tooltip__trigger___79833">
                        <span>
                            <svg>
                                <path stroke-width="11" >
                                </path>
                            </svg>
                        </span>
                    </div>
                </div>

                <div class="box--obj__type  ellipsis">          
                    <span class="js-tooltip">
                        <span class="js-tooltip__trigger" id="js-tooltip__trigger___21209">Available</span>
                    </span>     
                </div>
            </div>
        </a>
    </div>
</div>
2 Answers

You are looking for something like this

'//a[@class="clean"][ancestor::div[@data-passendheid="Correct"]][//*[local-name()="path"][@stroke-width="11"]][//span[@class="js-tooltip"][contains(.,"Available")]]'

    target element  |           parent element                  |      child element with stroke-width        |        child element with text Available

This should work accordingly to all your requirements:

"//div[@data-passendheid='Correct' and .//*[name()='path'][@stroke-width='11'] and .//span[@class='js-tooltip']/span[text()='Available']]//a[@class='clean' and @href='url']"
Related