繁体   English   中英

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

[英]how can i click outer div by find element of inner div

我想通过使用内部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>

通过使用

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

我想点击其上格

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

所有的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标签。

要使用文本在<h3>节点上定位先前的<div> ,可以使用以下“ 定位器策略”

  • 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]"))) 

您可以使用类似:

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

演示:

在此处输入图片说明

参考文献:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM