1

I want to click the outer div by using element of inner div. I have only the name by which I can find inner div

<div class="item fadein">
    <article class="non-existent" id="tmdb-82702">
        <div class="image">
            <img src="#">
            <div id="cimprt-82702" class="cimport" data-tmdb="82702" data-type="movie"></div>
        </div>
        <div class="data">
            <h3>How to Train Your Dragon 2</h3>
            <span>Jun. 13, 2014</span>
        </div>
    </article>
</div>

by using

find_element_by_xpath("//h3[contains(text(),'How to Treain Your Dragon 2')]")

I want to click its upper div i.e

<div id="cimprt-82702" class="cimport" data-tmdb="82702" data-type="movie"></div>

all div's are pack in

<div class="item fadein">
JeffC
  • 22,180
  • 5
  • 32
  • 55

3 Answers3

1

One way to do this is to use this XPath

//article[.//h3[.='How to Train Your Dragon 2']]//div[@data-type='movie']
^ start by finding an ARTICLE tag
         ^ that contains an H3 tag with the movie name
                                                ^ then from the ARTICLE tag, find the DIV that has the data-type='movie'

I tested this on the HTML provided and it found your desired DIV tag.

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • @DebanjanB Any reason why... other than I downvoted your answer? I put the provided HTML into a sample page and tested it in Chrome and it works just fine. Did you actually test it or just guessing? – JeffC Aug 25 '19 at 21:52
1

To locate the previous <div> with respect the <h3> node with text you can use the following Locator Strategies:

  • XPath 1:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h3[text()='How to Train Your Dragon 2']//preceding::img[1]//following::div[@class='cimport']")))
    
  • XPath 2:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h3[text()='How to Train Your Dragon 2']//preceding::img[1]//following::div[1]")))
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Your XPath is not valid... did you test it? – JeffC Aug 25 '19 at 21:11
  • No... I did check it on the posted HTML and it came back with XPath is not valid. I used the Chrome dev tools like I always do. – JeffC Aug 25 '19 at 21:51
  • @JeffC Why _Chrome dev tools_ when there are well tested _xpath checkers_ available with a simple google search? – undetected Selenium Aug 25 '19 at 22:18
  • Because Chrome's XPath engine is what counts... not some random XPath checker on the web. No one writes automation against an XPath checker, they write it against Chrome or some other browser. And how are you going to google the XPath checker...? with Chrome? So why do you need an XPath checker at all? – JeffC Aug 25 '19 at 22:35
  • @JeffC Rather than a comment, convert your comment into a legitimate question, stackoverflow contributors will be happy to help you out. – undetected Selenium Aug 25 '19 at 23:06
  • You were the one with the question... I was just answering. Since you seem to be the one with all the questions, post a new question and stackoverflow contributors will be happy to help YOU out. – JeffC Aug 25 '19 at 23:30
0

You could use something like:

//div[contains(normalize-space(),'How to Train Your Dragon 2')]/descendant::div[@data-type='movie']

Demo:

enter image description here

References:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133