xpath根据内部html子标记文本匹配specfic元素

时间:2017-10-13 12:12:36

标签: python xpath selenium-webdriver

我有一个html,如下所示

<div class="xtree">
       <img class="dojoimg">
       <span class="presentation">+</span>
 <span class ="treenode">
  <div class="ctreefolder">.... </div>
    <div class="presentationfolder">.... </div>
  <span >Setting</span>
 </span>
</div>
<div class="xtree">
       <img class="dojoimg">
       <span class="presentation">+</span>
 <span class ="treenode">
  <div class="ctreefolder">.... </div>
    <div class="presentationfolder">.... </div>
  <span >Home</span>
</span>
</div>
<div class="xtree">
       <img class="dojoimg">
       <span class="presentation">+</span>
 <span class ="treenode">
  <div class="ctreefolder">.... </div>
    <div class="presentationfolder">.... </div>
  <span >products</span>
</span>

</div>

我想根据最后一个span标记中的文本单击img图标。

例如,我想选择第一个img标签,如果最后一个跨度包含&#34;设置&#34; 。你能帮我写一下这个UI元素的xpath,以便在selenium webdriver python中使用

2 个答案:

答案 0 :(得分:0)

这是我的解决方案:

使用 find_element_by_link_text

# create a hash with our regular expressions and replacements
map = { /_(.+?)_/ => '<em>\1</em>', /\*(.+?)\*/ => '<strong>\1</strong>'}
markdown = "hallo _html_ dan *gaga* das *test*"
# enumerate the hash and replace the expressions with their replacement
# tap returns the result to itself
markdown.tap{|md| map.each { |re, v|  md.gsub!(re, v)}}
# => hallo <em>html</em> dan <strong>gaga</strong> das <strong>test</strong>

答案 1 :(得分:0)

我认为这个XPath会帮助你。在这里我找到img类然后匹配文本包含

//*[@class="dojoimg"]//span[contains(text(), "Setting")]
  

希望这个概念对你有帮助。