get info from page using selenium - c#

in a page there is some tags like this:
<tr class=" ev_modern">
<td align="left" valign="middle" title="1">1</td>
<td align="left" valign="middle" title="09:00:08" class="">09:00:08</td>
<td align="left" valign="middle">3000</td>
<td align="left" valign="middle" title="2539.00">2539.00</td>
</tr>
I looking for(row number, time, number1, number2) each of them, and tried many ways but I couldn't, one of them:
var elements = driver.FindElements(By.CssSelector("[class=' ev_modern']"));
elements is empty
how get information by selenium using C#?

Use following css selector to get to the td elements:
By.CssSelector("[class=' ev_modern'] td")
Then you can iterate through the list of elements.
Or if you want to get text from first row, do following:
driver.FindElement(By.CssSelector("[class=' ev_modern'] td:nth-child(1)")).Text;
Hope this helps.

Related

How can I get the XPath of a Button in a cell with a non-unique parent cell

I am struggling to figure out how to access a button in a table cell. The HTML outline looks like this:
<tr role="row" class="odd">
<tr role="row" class="even">
<tr role="row" class="odd>
<tr role="row" class="even">
<td class="">6/27/2017</td>
<td>1234567</td>
<td>USG</td>
<td>Buy Settlement</td>
<td class="sorting_1">$1.00</td>
<td></td>
<td></td>
<td>
Reconcile
</td>
</tr>
<tr role="row" class="odd">
<tr role="row" class="even">
<tr role="row" class="odd">
<tr role="row" class="even">
Each tr has the same cell structure except for the information inside is different for every cell.
I am trying to access the link in the last td element however cannot seem to find any xpath that is valid because I do not have a unique parent cell. The '1234567' cell value is a unique identifier but I have not found how to use an XPath for a sibling node.
Can anyone help me?
This XPath,
//tr[td='1234567']/td/a[.='Reconcile']
will select the a element whose string value is Reconcile and which exists in a tr that contains a td with a string value of 1234567.
I see to possible solutions for this:
first:
//a[contains(#onclick, "bellLoadModal")]. This will have multiple results. Iterate them if you know what link-GUID you're lookin for.
second:
//tr[#role="row"]/td[1]. This will result in the nodes with the IDs. Iterate them checking for the ID you're lookin for and then go one node up from the current element and select the 7th child node. This is the one with the link.
I think the below XPath should work:
(//a[#class='btn btn-default btn-xs']) [last()]
If your requirement is for "the link in the last column of the row whose second column contains "1234567", then that's
//tr[td[2] = "1234567"]/td[last()]/a/#href

Selenium Webdriver using C#: How to find a URL from number of URL on a webpage and click on it?

I am writing a C# script in selenium websdriver to test a website and got stuck in between. I have a multiple URL's on a webpage and I need to find one URL out of them and apply a click event on it. The URL names are dynamic hence its difficult for me to find it using 'ID' or 'name'. The URL name can be anything for example: the_rise_of_India.htm or the_rise_india.htm or the_riseIndia.htm etc... How can I filter and find my desired URL and click on it?
Kindly help me on the above scenario.
My HTML code is:
<table xmlns:downloader="http://schemas.niku.com/java/com.niku.dms.web.ZipPageDownloader" border="0" cellspacing="1" cellpadding="2" class="tableGridList" width="100%">
<tbody>
<tr class="TableGridList"><td class="ColHeadNoSort" scope="col"><img src="ui/evolution1/images/IcoCheckAll.gif" border="0" alt="Check All" title="Check All"></td><td class="ColHeadNoSort" colspan="3" align="center" scope="col">Name</td><td class="ColHeadNoSort" align="center" scope="col"> </td><td class="ColHeadNoSort" scope="col">Size</td><td class="ColHeadNoSort" scope="col">Type</td><td class="ColHeadNoSort" scope="col">Status</td><td class="ColHeadNoSort" scope="col">Modified</td><td class="ColHeadNoSort" scope="col">Actions</td></tr>
<tr valign="top" class="rowOff" onmouseover="this.className='rowOn'" onmouseout="this.className='rowOff'"><td valign="middle" align="center" width="20" scope="row"> </td><td valign="middle" width="20" class="hierTee"><img src="ui/evolution1/images/Spacer.gif" height="16" width="16" border="0" alt=""></td><td valign="middle"><img src="ui/evolution1/images/fmFolderClosed.gif" alt="" title=""></td><td valign="middle" width="100%"><a class="tableLink" target="" href="app?action=dms.ProjectsfileManager&folderId=5697033&returnAction=dms.ProjectsFileManager&cancelAction=dms.ProjectsFileManager&actionItemId=&id=5103184&type=Projects&taskID=&fromPage=&rootFolderId=&">Great India's place</a></td><td valign="middle" align="left" nowrap="true" id="PPP"></td><td valign="middle" align="left" nowrap="true"></td><td valign="middle" align="right" nowrap="true"></td><td valign="middle" align="right" nowrap="true"></td><td valign="middle" align="center" nowrap="true">9/26/15 8:27 PM</td><td valign="middle" align="right"><select name="folder5697033" onchange="optionGoTo( this.form.name,'folder5697033')" class="docMgrAction"><option value=""></option></select></td></tr>
<tr valign="top" class="rowOff" onmouseover="this.className='rowOn'" onmouseout="this.className='rowOff'"><td valign="middle" align="center" width="20" scope="row"> </td><td valign="middle" width="20" class="hierTee"><img src="ui/evolution1/images/Spacer.gif" height="16" width="16" border="0" alt=""></td><td valign="middle"><img src="ui/evolution1/images/fmFolderClosed.gif" alt="" title=""></td><td valign="middle" width="100%"><a class="tableLink" target="" href="app?action=dms.ProjectsfileManager&folderId=5687045&returnAction=dms.ProjectsFileManager&cancelAction=dms.ProjectsFileManager&actionItemId=&id=5103184&type=Projects&taskID=&fromPage=&rootFolderId=&">India's silver gold awards</a></td><td valign="middle" align="left" nowrap="true" id="PPP"></td><td valign="middle" align="left" nowrap="true"></td><td valign="middle" align="right" nowrap="true"></td><td valign="middle" align="right" nowrap="true"></td><td valign="middle" align="center" nowrap="true">8/6/15 12:04 PM</td><td valign="middle" align="right"><select name="folder5687045" onchange="optionGoTo( this.form.name,'folder5687045')" class="docMgrAction"><option value=""></option></select></td></tr>
<tr valign="top" class="rowOff" onmouseover="this.className='rowOn'" onmouseout="this.className='rowOff'"><td valign="middle" align="center" width="20" scope="row"> </td><td valign="middle" width="20" class="hierTee"><img src="ui/evolution1/images/Spacer.gif" height="16" width="16" border="0" alt=""></td><td valign="middle"><img src="ui/evolution1/images/fmFolderClosed.gif" alt="" title=""></td><td valign="middle" width="100%"><a class="tableLink" target="" href="app?action=dms.ProjectsfileManager&folderId=5693965&returnAction=dms.ProjectsFileManager&cancelAction=dms.ProjectsFileManager&actionItemId=&id=5103184&type=Projects&taskID=&fromPage=&rootFolderId=&">India's Gold awards</a></td><td valign="middle" align="left" nowrap="true" id="PPP"></td><td valign="middle" align="left" nowrap="true"></td><td valign="middle" align="right" nowrap="true"></td><td valign="middle" align="right" nowrap="true"></td><td valign="middle" align="center" nowrap="true">9/8/15 10:02 AM</td><td valign="middle" align="right"><select name="folder5693965" onchange="optionGoTo( this.form.name,'folder5693965')" class="docMgrAction"><option value=""></option></select></td></tr>
<tr valign="top" class="rowOff" onmouseover="this.className='rowOn'" onmouseout="this.className='rowOff'"><td valign="middle" align="center" width="20" scope="row"> </td><td valign="middle" width="20" class="hierTee"><img src="ui/evolution1/images/Spacer.gif" height="16" width="16" border="0" alt=""></td><td valign="middle"><img src="ui/evolution1/images/fmFolderClosed.gif" alt="" title=""></td><td valign="middle" width="100%"><a class="tableLink" target="" href="app?action=dms.ProjectsfileManager&folderId=5691948&returnAction=dms.ProjectsFileManager&cancelAction=dms.ProjectsFileManager&actionItemId=&id=5103184&type=Projects&taskID=&fromPage=&rootFolderId=&">Awards night - India</a></td><td valign="middle" align="left" nowrap="true" id="PPP"></td><td valign="middle" align="left" nowrap="true"></td><td valign="middle" align="right" nowrap="true"></td><td valign="middle" align="right" nowrap="true"></td><td valign="middle" align="center" nowrap="true">8/28/15 7:30 AM</td><td valign="middle" align="right"><select name="folder5691948" onchange="optionGoTo( this.form.name,'folder5691948')" class="docMgrAction"><option value=""></option></select></td></tr>
</tbody>
</table>
The URL names are dynamic and I need to search the string and click on it.
SO if I understand your question properly , You just need to get the list of all the displayed URLS and click on your desired one.
1 st easy way.
Webelement element = driver.findElement(By.xpath("//*[text()='the_right_url.htm']"));
you can put a check here if you want to verify the URL is displayed on the page
by
if(element!=null)
element.click()
else
//test fails
the 2nd way you can get the list of URLS which are displayed on the page and then click on the desired one by index number
Webelements [] ele = driver.findElements(By.xpath(//*/li)) //let say the URLs are coming under li tags
now you can navigate through the list ele using loop may be for loop and get the index number(using come count let say count) where your desired url is displaying.
and then click on it using
driver.findelemenet(By.xpath(//*/li[count]));
If you have text that you know will always appear in the link you can find it by partial text
driver.FindElement(By.PartialLinkText("india"));
Or using contains
driver.FindElement(By.Xpath("//[contains(text(), 'india')]"));
Please note both options are case sensitive.

Xpath also parent child contains spesific value

I have a html structure as below
<tr valign="middle" align="left">
<td> </td>
<td class="table_row"><div align="left"></div></td>
<td class="table_row"><div align="right" class="news_headline_txt"> </div></td>
<td class="table_row"><div align="right">28.02.2013</div></td>
<td class="table_row"><div align="right"><a onClick="window.open('show.php?id=id_9000')" href="javascript:;"></a></div></td>
<td> </td>
</tr>
I can get onclick value as below
"//a[contains(#onclick, 'id_')]/#href
but when i get the onclick value, i want match tr's third td values <td class="table_row"><div align="right">28.02.2013</div></td>contains 02.03
Thanks in advance
So you want to find a TR that has a td with a certain position, and then find the anchor within that:
//tr[td[position()=4][contains(text(), '02.03')]]//a[contains(#onclick, 'id_')]/#href
You could also use the sibling axis:
//td[position()=4][contains(text(), '02.03')]/following-sibling:td//a[contains(#onclick, 'id_')]/#href
Note the position check is for the 4th td, since there is a blank padding td at the beginning of the row.

Can I use HtmlAgilityPack to split an HTML document on a certain tag?

For example, I have a bunch of <tr> tags I'd like to collect. I need to split each of these tags into individual elements, for easier parsing on my part.
Is this possible?
An example of the markup:
<tr class="first-in-year">
<td class="year">2011</td>
<td class="img"><a href="/battlefield-3/61-27006/"><img src=
"http://media.giantbomb.com/uploads/6/63038/1700748-bf3_thumb.jpg" alt=""></a></td>
<td class="title">
Battlefield 3
<p class="deck">Battlefield 3 is DICE's next installment in the franchise and
will be on PC, PS3 and Xbox 360. The game will feature jets, prone, a
single-player and co-op campaign, and 64-player multiplayer (on PC). It's due out
in Fall of 2011.</p>
</td>
<td class="date">Expected: Q4 2011</td>
<td>PC, <a href="/xbox-360/60-20/" class=
"X360">X360</a>, PS3</td>
</tr>
<tr>
<td class="year"></td>
<td class="img"><a href="/forza-motorsport-4/61-33400/"><img src=
"http://media.giantbomb.com/uploads/0/1992/1654849-forza4_thumb.jpg" alt=
""></a></td>
<td class="title">
Forza Motorsport 4
<p class="deck">The next installment of Turn 10's racing franchise slated for
release in Fall 2011. It is set to feature 16 player online races, dynamic race
conditions, cars from over 80 manufacturers, and compatibility with Kinect, both
on and off the racetrack.</p>
</td>
<td class="date">Expected: Oct 2011</td>
<td>X360</td>
</tr>
<tr>
<td class="year"></td>
<td class="img"><a href="/max-payne-3/61-23398/"><img src=
"http://media.giantbomb.com/uploads/0/1400/938434-custom_1237811317319_mp3_poster_thumb.jpg"
alt=""></a></td>
<td class="title">
Max Payne 3
<p class="deck">The long awaited third instalment in Remedy's beloved series, in
which an aging Max Payne faces one final chance to redeem himself.</p>
</td>
<td class="date">Expected: 2011</td>
<td>PC, <a href="/playstation-3/60-35/" class=
"PS3">PS3</a>, X360</td>
</tr>
So I would have three elements here for this example. :)
You can't split it into multiple HTML documents on the tag if that's what you mean. You can select the individual TD elements and parse those separately.
The XPath selector //td will select all elements which you can pass into a parsing method.
HtmlAgilityPack.HtmlDocument doc = LoadHtmlHowever();
doc.DocumentNode.SelectNodes("//td");

Extract region and replace them back for a template

I have a template where I want to replace certain regions. In my example below, I want to extract the regions between the ... comments, manipulate it, then replace them back after the manipulation.
I do not need the logic to merge the fields, but I need to extract the regions so I can use my logic and place it back into the template.
Does anyone know of an elegant or simple way to extract these regions? I am also hoping to extract the url values in the process as well if it is easy to do along the way.
<table width="700" border="0" align="center" cellpadding="4" cellspacing="0">
<tr>
<td align="center" valign="top">
<!--DynamicSlotStart url="http://www.test.com/itemdisplay0_10751_-1_57436_10001"-->
<table>
<tbody>
<tr>
<td><p><a title="[element='title']" href="[url]"><img border="0" alt="[element='title']" src="[element='photo' property='src' maxwidth='135']" width="135" height="135" /></a></p></td>
</tr>
<tr>
<td><span>[element='h1']</span></td>
</tr>
<tr>
<td><span><strong>[element='price']<br />
</strong></span><span>[element='was_price']</span></td>
</tr>
<tr>
<td><span><a title="[element='title']" href="[url]">Details</a></span></td>
</tr>
</tbody>
</table>
<!--DynamicSlotFinish-->
</td>
<td align="center" valign="top">
<!--DynamicSlotStart url="http://www.test.com/itemdisplay0_10751_-1_3379_10001"-->
<table>
<tbody>
<tr>
<td><p><a title="[element='title']" href="[url]"><img border="0" alt="[element='title']" src="[element='photo' property='src' maxwidth='135']" width="135" height="135" /></a></p></td>
</tr>
<tr>
<td><span>[element='h1']</span></td>
</tr>
<tr>
<td><span><strong>[element='price']<br />
</strong></span><span>[element='was_price']</span></td>
</tr>
<tr>
<td><span><a title="[element='title']" href="[url]">Details</a></span></td>
</tr>
</tbody>
</table>
<!--DynamicSlotFinish-->
</td>
<td align="center" valign="top">
<!--DynamicSlotStart url="http://www.test.com/itemdisplay0_10751_-1_104854_10001"-->
<table>
<tbody>
<tr>
<td><p><a title="[element='title']" href="[url]"><img border="0" alt="[element='title']" src="[element='photo' property='src' maxwidth='135']" width="135" height="135" /></a></p></td>
</tr>
<tr>
<td><span>[element='h1']</span></td>
</tr>
<tr>
<td><span><strong>[element='price']<br />
</strong></span><span>[element='was_price']</span></td>
</tr>
<tr>
<td><span><a title="[element='title']" href="[url]">Details</a></span></td>
</tr>
</tbody>
</table>
<!--DynamicSlotFinish-->
</td>
<td align="center" valign="top">
<!--DynamicSlotStart url="http://www.test.com/itemdisplay0_10751_-1_80977_10001"-->
<table>
<tbody>
<tr>
<td><p><a title="[element='title']" href="[url]"><img border="0" alt="[element='title']" src="[element='photo' property='src' maxwidth='135']" width="135" height="135" /></a></p></td>
</tr>
<tr>
<td><span>[element='h1']</span></td>
</tr>
<tr>
<td><span><strong>[element='price']<br />
</strong></span><span>[element='was_price']</span></td>
</tr>
<tr>
<td><span><a title="[element='title']" href="[url]">Details</a></span></td>
</tr>
</tbody>
</table>
<!--DynamicSlotFinish-->
</td>
</tr>
</table>
Maybe this project will be helpful: Html Agility Pack
What is exactly the Html Agility Pack (HAP)?
This is an agile HTML parser that builds a read/write DOM and supports plain XPATH or XSLT (you actually don't HAVE to understand XPATH nor XSLT to use it, don't worry...). It is a .NET code library that allows you to parse "out of the web" HTML files. The parser is very tolerant with "real world" malformed HTML. The object model is very similar to what proposes System.Xml, but for HTML documents (or streams).
Html Agility Pack now supports Linq to Objects (via a LINQ to Xml Like interface). Check out the new beta to play with this feature
Sample applications:
Page fixing or generation. You can
fix a page the way you want, modify
the DOM, add nodes, copy nodes,
well... you name it.
Web scanners.
You can easily get to img/src or
a/hrefs with a bunch XPATH queries.
Web scrapers. You can easily scrap
any existing web page into an RSS
feed for example, with just an XSLT
file serving as the binding. An
example of this is provided.

Categories