简体   繁体   English

在C#Selenium Webdriver中使用XPath查找元素

[英]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. 我的源代码中有这个元素,我得到带有内部text(yahoo)的h2元素,并且我想访问包含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[text()='yahoo']//..//..但它不起作用。

这个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']//..//..//.. 请使用此xpath并尝试: //h2[text()='yahoo']//..//..//..

Explanation: 说明:

1st //.. is taking you to the anchor(a) tag. 1st //..带您进入anchor(a)标签。

2nd //.. is taking you to header tag. 第二个//..带您进入标头标记。

3rd //.. will take you where you want to be ie article tag. 3rd //..将带您到达想要的位置,即商品标签。

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

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