Python 如何通过查找内部div的元素来单击外部div
我想使用内部div的元素单击外部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 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']
演示:
参考资料:
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']