How do I make sure that my selenium code scrapes all matching contents of my XPath?
Please help me with your ideas.
For example, these are my HTML Tags:
<tr class="1" role="r1">
<td class="c1">
<a href="www.google.com">
</a>
</td>
</tr>
<tr class="2" role="r2">
<td class="c2">
<a href="www.youtube.com">
</a>
</td>
</tr>
<tr class="3" role="c3">
<td class="c3">
<a href="www.facebook.com">
</a>
</td>
</tr>
I want my selenium code to fetch all links from href tag.
So, below is my XPath:
String links = driver.findElement(By.xpath("//tr[#role='cad']//td[#class='c1']//a")).getAttribute("href");
System.out.println(links);
but it fetches only the first href output, i.e. www.google.com.
The desired output is:
www.google.com
www.youtube.com
www.facebook.com
How can I achieve this?
Any array implementation would be better options?
Try Following code:
List<WebElement> elements= driver.findElements(By.xpath("//table/tbody/tr"));
int i =0 ;
while(i<elements.size()){
WebElement childElement = elements.get(i).findElement(By.cssSelector("a"));
System.out.println(childElement.getAttribute("href"));
i++;
}
Try below code.
List<WebElement> links = driver.findElements(By.xpath("//tr/td/a"));
for(int i=0;i<links.size();i++){
System.out.println(links.get(i).getAttribute("href"));
}
Related
I am not able to access the webtable elements inside the table2 , please see the page source below :
<table id="table1">
<tr class="head">
<td class="left" colspan="2">
<!--PAGE LINKS-->
</td>
</tr>
<tr>
<td class="left_link">
<h1>
<a name="nav_home" href="index.html" id="home_link" class="home">HOME</a><br>
<a name="nav_adopt" href="adoption.html" id="adoption_link">ADOPTION</a><br>
<a name="nav_about" href="about.html" id="about_link">ABOUT</a><br>
<a name="nav_contact" href="contact.html" id="contact_link">CONTACT</a><br>
</h1>
</td>
<td class="content">
<h1>
CONTACT US
</h1>
<hr>
<p>
Use the form below to contact us if you have any questions, queries or even any requests.
We are always happy to hear from you all.
</p>
<h1 class="subhead">Contact Form</h1>
<form name="message_form">
<table id="table2" class="inner_table">
<tr>
<td>Enter Name</td>
<td><input type="text" name="name_field"></td>
</tr>
<tr>
<td></td>
<td>
<input type="radio" id="rinfo" name="rbutton" value="information">Information
<input type="radio" id="rdona" name="rbutton" value="Donation">Donation
<input type="radio" id="radop" name="rbutton" value="Donation">Adoption
</td>
</tr>
For example i am not able to access the name inside the table2.
I tried accessing using below but failed .
driver.findElements(By.xpath("//*[#class='table1']/tbody/tr[2]/td[2]/form/table[#class='inner_table']/tr[1]/td[1))
Try this below xpath
//table[#id='table2']/..//following::input[#name='name_field']
Explanation of xpath:- Use id attribute of <table> tag and move ahead with <input> tag using following keyword.
This above xpath will locate the Enter Name field text box.
Note:- Instead of using absolute xpath, Use relative xpath.
It is not recommended to use xpath on selenium, since elements might tend to change and you have to refactor your selector if you add another element.
To answer your question, you can use the id for table2 and select the desired element.
It should be a CSS selector as simple as input[name='name_field'] or if that isn't specific enough, use #table2 input[name='name_field'].
Try xpath like below:
to access input filed with name 'name_field' use below xpath
driver.findElement(By.xpath("//table[#id='table2' and #class= 'inner_table']//input[#type='text' and #name='name_field']")
Hope it will help.
In web tables, you can use xPath to reach parent of an element and then finding the preceding-sibling or the following-sibling of td tags
This tutorial will help you:
http://www.seleniumtests.com/2011/10/using-xpath-to-reach-parent-of-element.html
I'm newby in selenium automation.
I have one scenario where I have to click on a button based on some title. but the button element looking same for all all rows.
This is the image:
this is html for the same-
<tr class="odd gradeX">
<td class="hide"></td>
<td>My sample Test</td>
<td> Priyank pareek </td>
<td style="width:50px;text-align:center;">01/11/2017</td>
<td style="width:50px;text-align:center;"> Approved </td>
<td style="width:50px;text-align:center;">
<td style="width:100px;">
<a class="btn btnReject red btn-sm btn-outline sbold uppercase" href="javascript:;" onclick="updateTest(this)" data-status="0" data-gui="6438C99F-E166-49DA-8B89-DD0E2EF33A62" title="Reject">
</td>
</tr>
How can i click on the reject button ? please help me
You can use below XPath:
WebElement element = driver.findElement(By.xpath("//a[#title='Reject']"));
or CSS selector
WebElement element = driver.findElement(By.cssSelector("a[title='Reject']"));
If these selectors still matches multiple elements, try:
WebElement element = driver.findElement(By.xpath("//tr[td[text()='My sample Test']]//a[#title='Reject']"));
Try to locate using following xpath -
//td[text()='My sample Test']/following-sibling::td/a[#title='Reject']
Explanation :-
Find the td tag which having text as 'My sample Test'
following-sibling used to navigate the sibling which have reject button
You can search using class name btnReject. In Python, I usually use driver.getElementByClassName("btnReject").click(). You can change them into Java to use.
Below is the html code... I want to know which xpath can I use to select such a check box.
In actual HTML code I have multiple td for checkboxes, here I have shown only one.
I already tried
driver.findElement(By.id("cntMain_GridView1_chIndividual_0")).click();
but it gives an error saying no such element found on web page.
<table id="cntMain_GridView1" class="table iconTwo" cellspacing="0" cellpadding="3" style="width:100%;border-collapse:collapse;">
<tbody>
<tr>
<td class="no-sorting" valign="top" align="left" style="width:10px;">
<p class="defaultP" tabindex="35">
<span style="display:inline-block;width:5%;">
<div class="ez-checkbox">
<div class="ez-checkbox">
<input id="cntMain_GridView1_chIndividual_0" class="ez-hide" type="checkbox" onclick="javascript:RbtnOnOff(this.id);" name="ctl00$cntMain$GridView1$ctl02$chIndividual" tabindex="-1"/>
</div>
</div>
</span>
</p>
</td>
</tr>
</tbody>
</table>
Try changing the display style property of the element and then clicking on the element. Use JavascriptExecutor to make the element visible. Try the following code:
JavascriptExecutor js = (JavascriptExecutor) driver;
String domain_name = (String) js.executeScript("return document.getElementById(" +
"'cntMain_GridView1_chIndividual_0').style.visibility = 'visible'");
driver.findElement(By.id("cntMain_GridView1_chIndividual_0")).click();
I am looking for the help to read a table span using selenium code and write span into the file,
following is my html code
<table>
<tbody><tr>
<tr>
<td><span>
FIRST
</span>
</td>
</tr>
<tr>
<td>
<span>SECOND</span>
</td>
</tr>
<tr>
<td>
<span>THIRD</span>
</td>
</tr>
<tbody>
</table>
I need to write FIRST SECOND THIRD on a file in java.
Thanks a lot.
I suppose you have located/found the table WebElement. Then you can get span elements content like this:
List<WebElement> spanElements = tableElement.findElements(By.ByTagName("span"));
for (WebElement element : spanElements) {
String spanContent = element.getText();
//save it to a collection or a StringBuilder, then write it to a file
}
Having a look at this and this might help.
1) XPath that gets all text is:
//span/text()
2) in java code you may type something like
String text = selenium.getText("xpath=//span");
I have the following HTML
<html>
<head>
<title>test</title>
</head>
<body>
<table>
<caption>table title and/or explanatory text</caption>
<thead>
<tr>
<th>header</th>
</tr>
</thead>
<tbody>
<tr>
<td id=\"test\" width=\"272\"></td>
</tr>
</tbody>
</table>
Test link
<img src=\"http://www.google.se/images/nav_logo95.png\" />"
</body>
</html>;
And I want to find the first link with jsoup and replace it with a text
Element elem = page.select("a[href=" + link.getUrl() + "]:contains(" + link.getName() + ")").first();
I can only replace the inner HTML with elem.html("foo") or print the outerHtml with elem.outerHtml()
Does anyone know how I can achieve this?
I found the answer!
TextNode text = new TextNode("foo", "");
elem.replaceWith(text);
Once you have found the element that you want to work with, you may apply the commands such as explained here: http://jsoup.org/cookbook/modifying-data/set-html
I could not get it right. I am trying this:
elemento.prepend("<a href='www.test.com'>");
elemento.html("Roberto C. Santos.");
elemento.append("</a>");
elemento.wrap("<a href='www.test.com'> </a>");
But I am getting this:
<td> <a style="" target="_self" title="" href="http://ivv.veveivv.vvzenes.com.br/mao/ara/ccacao" data-mce-href="resolveuid/5cc1c7c8c9efcacaaeedec22a9c69a54" class="internal-link">Roberto C. Santos.</a></td>
</tr>
I still don´t know the exact way to exchange the contents of an URL element.
I´d like to have, as the result:
<a href='www.test.com'> Roberto C. Santos.</a>"
How coul´d I erase the href that is inbetween?