简体   繁体   中英

Find element with xpath in c# selenium webdriver

I have this element in my source code, I get h2 element with inner text(yahoo), and I want to access the nearest article that contains h2.

<article>
 <div></div>
 <div></div>
 <header>
   <a></a>
   <a>
     <h2>yahoo</h2>
  </a>
 </header>
</article>

my written XPath is this : //h2[text()='yahoo']//..//.. but it doesn't work.

这个xpath怎么样:

//h2[. = 'yahoo']/ancestor::article[1]

获得包含<h2>元素的<article>元素的一种可能的XPath等于yahoo

//article[.//h2='yahoo']

如果您想与文章打交道,请使用下一个字符串:

//article[header//h2[text='yahoo']]

Please use this xpath and try : //h2[text()='yahoo']//..//..//..

Explanation:

1st //.. is taking you to the anchor(a) tag.

2nd //.. is taking you to header tag.

3rd //.. will take you where you want to be ie article tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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