Find element with xpath in c# selenium webdriver - c#

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.

How about this xpath:
//h2[. = 'yahoo']/ancestor::article[1]

One possible XPath to get <article> elements containing <h2> element equals yahoo :
//article[.//h2='yahoo']

If you're want to interract with article, use next string:
//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 i.e article tag.

Related

Selenium C#: Get Element by title

I want to get an element by its title, I don't know if we have a universal solution for this in Selenium C#.
Here is the example of the HTML
<div class="student">
<a href="abcxyz.com">
<span class="title"> ZZZ</span>
</a>
</div>
And another example
<div class="page">
</div>
Of course if just one page, I will do it like
FindElement(By.ClassName("page")).GetAttribute("href");
or
FindElement(By.ClassName("student")).GetAttribute("href")
to get the "href" out of where the "title" is located. But they have pages with different designs, and the only common thing between them is that title. I wonder if there is any solution for me to find the "href" by the "title=ZZZ"?
Thanks in advance.
The two examples are different, I would use a union (|) operator of XPath expressions that will work in both cases. If you have more cases, you can add additional union operator, followed by the new case XPath:
//a[#title='ZZZ']/#href | //*[#class='title'][contains(text(),'ZZZ')]/parent::a/#href
Without searching for specific text:
//a[#title]/#href | //*[#class='title']/parent::a/#href
Try this xpath:
/a[#title="ZZZ"]/#href

How to get xpath of different html values with the same properties

I'm working on Selenium and trying to get the values inside tags. The site that I'm working on is https://www.qnbfinansbank.enpara.com/doviz-kur-bilgileri/doviz-altin-kurlari.aspx. But the properties of the objects are the same. Therefore, the xpath scripts are the same. The values that I'm trying to get are like 5,615505 TL, 4,827450 TL, 187,389825 TL from
<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>
and so on. Is there any way to get the xpath of these values?
You can store all the values in a List. Then one by one you can retrieve it.
Something like :
IList<IWebElement> allValues= driver.FindElements(By.CssSelector("div.dlCont span"));
foreach (IWebElement values in allValues)
{
Console.WriteLine(values.Text);
}
Hope this will help.
You can use like this,
//span[contains(text(),'5,615505 TL')]
You can manually write the xpath for the below DOM Structure
<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

Grabbing text of a single element in XDocument (C#)

I have an XML document that looks like
<a>foo<b>bar</b></a>
Creating an XDocument with the above XML, then using
doc.Descendants(new XName("a")).First().Value
results in "foobar" rather than "foo" as I expected.
How can I just get the value of <a /> without subtracting the value of <b /> from <a />?
Thanks in advance!
<a> actually contains two nodes, a text node and the b element. You can filter a children to of type XText:
var xml = "<a>foo<b>bar</b></a>";
var document = XDocument.Parse(xml);
Console.WriteLine(document.Descendants("a").First().Nodes().OfType<XText>().First().Value);
Seem kind of invalid XML. Maybe you should try attribute ... something like this
<a name="foo">
<b>bar</b>
</a>
This isn't quite an answer, but I would question the validity of your XML in this example.
Consider the XML-compatible markup for a HTML paragraph with a hyperlink:
<p>Go to the StackOverflow front page</p>
The contents of the paragraph (the value of <p> ) is still the full sentence rather than just the words "go to the ".
Also, given your example, what happens if you have more text after <b>bar</b>?

How Use From HTMLAgilityPack For Finding A String in nested divs

there is an html codes like below :
<div class="class1 class2 class3">
<div class="class4 class5">
<span class="class6">GOAL STRING</span>
</div>
</div>
now i want to find that GOAL STRING use from HTMLAgilityPack.
how can i do that?
[with LINQ and without LINQ = please show us both ways]
thanks in advance
Well you can use xpath to get the span directly.
document.DocumentNode.SelectSingleNode("//div[#class='class1 class2 class3']/div[#class='class4 class5']/span[#class='class6']").InnerText;
This is a good resource for xpath specifically the table in the middle of the page:
http://www.codeproject.com/Articles/9494/Manipulate-XML-data-with-XPath-and-XmlDocument-C
Also on Google Chrome you can right click -> inspect element and then right click the element that shows up on the tree and click copy as Xpath to get a starting point. These expressions can usually be simplified.

Extract content with XPath?

I have html content that I am storing as an XML document (using HTML Agility Pack). I know some XPath, but I am not able to zero into the exact content I need.
In my example below, I am trying to extract the "src" and "alt" text from the large image. This is my example:
<html>
<body>
....
<div id="large_image_display">
<img class="photo" src="images/KC0763_l.jpg" alt="Circles t-shirt - Navy" />
</div>
....
<div id="small_image_display">
<img class="photo" src="images/KC0763_s.jpg" alt="Circles t-shirt - Navy" />
</div>
</body>
</html>
What is the XPath to get "images/KC0763_l.jpg" and "Circles t-shirt - Navy"? This is how far I got but it is wrong. Mostly pseudo code at this point:
\\div[#class='large_image_display']\img[1][#class='photo']#src
\\div[#class='large_image_display']\img[1][#class='photo']#alt
Any help in getting this right would be greatly appreciated.
The following xpath will get you to the src attributes for the img tags:
'//html/body/div/img[#class="photo"]/#src'
And similarly this will get you to the alt attributes:
'//html/body/div/img[#class="photo"]/#alt'
From there you can get to the attribute text. If you want to only find the ones that match 'large_image_display' then you would filter it further like this:
'//html/body/div[#id="large_image_display"]/img[#class="photo"]/#src'
Use the following XPath expressions:
/html/body/div[#id='large_image_display']/img/#src
and
/html/body/div[#id='large_image_display']/img/#alt
Always try to avoid using the // abbreviation, because it may result in very inefficient evaluation (causes the whole (sub)tree to be scanned).
In this particular case we know that the html element is the top element of the document and we can simply select it by /html -- not //html.
Your major problem was that in your expressions you were using \ and \\ and there are no such operators in XPath. The correct XPath operators you were trying to use are / and the // abbreviation.

Categories

Resources