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

Time:01-11

I'm trying to press an element <a 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 > 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  data-passendheid="Correct">
  
     <div  style="">

        <a  href="url">

            <div >      
                      
                <div >
                    <div js-tooltip__trigger___79833">
                        <span>
                            <svg>
                                <path stroke-width="11" >
                                </path>
                            </svg>
                        </span>
                    </div>
                </div>

                <div >          
                    <span >
                        <span  id="js-tooltip__trigger___21209">Available</span>
                    </span>     
                </div>
            </div>
        </a>
    </div>
</div>

CodePudding user response:

You are looking for something like this

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

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

CodePudding user response:

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']"
  •  Tags:  
  • Related