I'm trying to get the right syntax with either XPATH or CssSelector using C# to extract the dollar amount shown below:
<div class="a-column a-span3 a-text-right a-span-last a-color-price a-text-bold sc-value">
<span class="a-size-base sc-price-sign">
<span class="a-nowrap">$39.93</span>
</span>
</div>
Your help would be greatly appreciated. Thank you.
There are many ways to match the element with the desired text in this case. Here is one of the approaches relying on more or less non-layout specific classes that have a relevant meaning:
driver.FindElement(By.CssSelector(".a-color-price > .sc-price-sign > span")).Text;
Related
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
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
i want to sendkeys "description" within a textarea. I have tried all the possible ways but does not work.
HTML of the element :
<div class="ta-scroll-window ng-scope ta-text ta-editor form-control" ng-hide="showHtml">
<div class="popover fade bottom" style="max-width: none; width: 305px;">
<div class="arrow"></div>
<div class="popover-content"></div>
</div>
<div class="ta-resizer-handle-overlay">
<div class="ta-resizer-handle-background"></div>
<div class="ta-resizer-handle-corner ta-resizer-handle-corner-tl"></div>
<div class="ta-resizer-handle-corner ta-resizer-handle-corner-tr"></div>
<div class="ta-resizer-handle-corner ta-resizer-handle-corner-bl"></div>
<div class="ta-resizer-handle-corner ta-resizer-handle-corner-br"></div>
<div class="ta-resizer-handle-info"></div>
</div>
<div id="taTextElement737852736512107" contenteditable="true" ta-bind="ta-bind" ng-model="html" ta-keep-styles="true" class="ng-pristine ng-valid ta-bind ng-empty ng-touched" an-form-object-name="Açıklama" name="Açıklama">
<p>
<br>
</p>
</div>
</div>
Code trial :
Dim action2 = New Actions(driver)
Dim cekbul2 = driver.FindElement(By.XPath("//*#id=""taHtmlElement737852736512107""]"))
cekbul2.SendKeys("Açıklama")
Console.Write("textarea send description")
or
Dim cekbul2 = driver.FindElement(By.XPath("//textarea[#class='ng-pristine ng-untouched ng-valid ng-scope ta-bind ta-html ta-editor form-control ng-empty ng-hide' and #id='taHtmlElement737852736512107']"))
The error is :
"no such element: Unable to locate element does not work" give error
Your html does not have a text area input field inside it.
When you use an xPath that says
'//textarea' this means that you are looking for an element that has tags of <textarea> </textarea>
It looks like your html is actually div's that are styled up to look like text areas.
That is why your second attempt will never work - because you are looking for a textarea where none exists.
Typically, in the situation where a div is styled up to work like a text area or textbox, you will find that the div has a backing input behind it.
These must be located between the
<form> and </form> tags in the html - otherwise the server would never be able to receive the data. (Html 5 provides new ways of working with this - but that is another story)
Can you examine your full html, and see if you can find the actual text area objects or the input type objects that end up containing the text content.
Type some dummy text, and use an html inspector tool within chrome or firefox to look for your dummy text.
If however, the post is completed by javascript - you may find that the javascript does not use inputs or text areas for containing the text and instead posts it external to any form elements. This is common with richtext emulators such as forum post pages.
If that is the case- you may need to experiment and find the appropriate html element that you need to send keys to in order for the content to work.
Also - could you try
Dim cekbul2 = driver.FindElement(By.XPath("//div[#id='taHtmlElement737852736512107']"))
I couldnt help but notice it had an xPath syntax error - you had no starting [ square bracket ] - also, in programming it is sometimes considered lazy a bad practice to wildcard / work with dynamics. I recommend always using the tag type for your xpaths, as opposed to '//*'
Worse case scenario, I would say that you could probably get around this by using Javascript execution. Eg: Directly setting the text, instead of 'sending the key strokes'.
However, this does not emulate human behavior - but it may be a necessary evil depending on your situation.
To send text to the <p> tag you have to use the ExecuteScript() method from IJavaScriptExecutor Interface and you can use the following code block :
((IJavaScriptExecutor)driver).ExecuteScript("document.getElementsByTagName("p")[0].innerHTML="Hasan Sarıkaya";");
I want to highlight some points here
Most probably your locator which you are using is not correct.
There are three way which I know to enter text using selenium
1)Use driver.findElement(yourLoator).sendKeys("Stringvalue");
2)You can use action class to send keys
3)You can use javascript executor to change innerHtml code
Personally ill not prefer the third solution, because we are testers I believe changing dom attribute is a good practice
Hope this will give you some help. please Let me know in case any query.
I am trying to write automated smoketest for an internal website. The problem is the website is almost primarily dynamically generated. So any kind of unique identifier like ID is a combination of known string prefix and ends with a radomized number.
I also can NOT depend on the order, so using things like div[2] or span[25] will not be reliable, UNLESS there is some way to grab the count of the span/div/input based on where I am currently located in the DOM by the KNOWN TEXT VALUE.
For example I can find the known text value. If I can somehow programatically determine that the span for this known text value is 55 , and I know that it is nested 2 deep from the other element I am looking for, then I could do something like "//span[55 - 2]/input".
The best I can do is navigate the DOM to some KNOWN text value, and work up OR down from there.
Given that, in the example below, how would I navigate to the INPUT element, when starting from the KNOWN TEXT VALUE???
<span id="RandomlyGenerated35673">
<span id="RandomlyGeneratedNum58532">
<span id="RandomlyGenerated78539">
<span/>
<span/>
<span id="RandomlyGenerated78539">KNOWN TEXT VALUE</span>
</span>
</span>
<input class="GENERIC-NON-UNIQUE" type="button" value="GENERIC-NON-UNIQUE"/>
</span>
You can use the following axis:
//span[. = 'KNOWN TEXT VALUE']/following::input[#type = 'button']
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.