Python 如何通过查找内部div的元素来单击外部div

Python 如何通过查找内部div的元素来单击外部div,python,selenium-webdriver,Python,Selenium Webdriver,我想使用内部div的元素单击外部div。我只有可以用来查找内部div的名称 <div class="item fadein"> <article class="non-existent" id="tmdb-82702"> <div class="image"> <img src="#"> <div id="cimprt-82702" class="cimport" dat

我想使用内部div的元素单击外部div。我只有可以用来查找内部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>
我要单击它的上分区,即

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

所有部门的人都进来了

<div class="item fadein">

一种方法是使用此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'

我在提供的HTML上对此进行了测试,它找到了所需的
DIV
标记。

要找到前面的
节点,您可以使用以下内容:

  • XPath 1:

  • XPath 2:


您可以使用以下内容:

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

参考资料:


您的XPath无效。。。你测试过了吗?没有。。。我确实在发布的HTML上检查了它,但它返回的XPath是无效的。我像往常一样使用了Chrome开发工具。@JeffC为什么要使用Chrome开发工具呢?因为Chrome的xpath引擎才是最重要的。。。不是web上的一些随机XPath检查器。没有人针对XPath检查器编写自动化,而是针对Chrome或其他浏览器编写自动化。你打算如何用谷歌搜索XPath检查器。。。?用铬?那么你为什么需要XPath检查器呢?@JeffC,把你的评论转换成一个合法的问题,stackoverflow贡献者会很乐意帮助你。@DebanjanB有什么原因吗。。。除了我否决了你的答案?我将提供的HTML放入一个示例页面,并在Chrome中进行了测试,效果很好。你是真的测试过还是只是猜测?
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']")))
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]")))
//div[contains(normalize-space(),'How to Train Your Dragon 2')]/descendant::div[@data-type='movie']