如何获得具有相同属性的不同html值的xpath

时间:2018-07-19 11:39:02

标签: c# selenium xpath

我正在研究Selenium,并尝试在标记内获取值。我正在使用的网站是https://www.qnbfinansbank.enpara.com/doviz-kur-bilgileri/doviz-altin-kurlari.aspx。但是对象的属性是相同的。因此,xpath脚本是相同的。我尝试获取的值例如来自以下国家/地区的5,615505 TL,4,827450 TL,187,389825 TL

<div class="dlCont">
<span>5,615505 TL </span>
</div>

<div class="dlCont">
<span>4,827450 TL </span>
</div>

<div class="dlCont">
<span>187,389825 TL </span>
</div>

,依此类推。有什么方法可以获取这些值的xpath?

3 个答案:

答案 0 :(得分:1)

您可以将所有值存储在列表中。然后可以一一检索。

类似:

IList<IWebElement> allValues= driver.FindElements(By.CssSelector("div.dlCont span"));
foreach (IWebElement values in allValues)
{
     Console.WriteLine(values.Text);
}  

希望这会有所帮助。

答案 1 :(得分:0)

您可以这样使用

//span[contains(text(),'5,615505 TL')]

答案 2 :(得分:0)

您可以手动为以下DOM结构编写xpath

        <div class="dlCont">
        <span>5,615505 TL </span>
         </div> 

Manually written xpath for above DOM structure is "//div[@class='dlCont']/span".

if the page is having many elements with same DOM struture then written Xpath will match with all the nodes.

There are 8 nodes are matched with XPATH="//div[@class='dlCont']/span" in the  below URL  https://www.qnbfinansbank.enpara.com/doviz-kur-bilgileri/doviz-altin-kurlari.aspx


     if you want to fetch particular webelements then you need to specify the index value as "(//div[@class='dlCont']/span)[2]". 

     you need to add open bracket in the starting of the manually written xpath and close bracket in the ending of the Xpath.after that you need to mention the index value 
    1.//div[@class='dlCont']/span
    2.(//div[@class='dlCont']/span
    3.(//div[@class='dlCont']/span)
    4.(//div[@class='dlCont']/span)[1]

    Hope it will be helpful