When i am trying to find number of tr(class-tr-heading) in the 1st table,i am getting 2 tr as a result if i use Xpath(working fine with CSS Selector-returns 1 tr)
<div class="large 20 columns">
<table class="Red"><tbody>
<tr class="tr-heading"></tr>
<tr></tr>
</tbody></table>
<table class="Red"><tbody>
<tr class="tr-heading"></tr>
<tr></tr>
</tbody></table>
</div>
Below one return two tr,
WebElement tbl=dr.findElement(By.xpath("//div[#class='large 20 columns']/table[1]/tbody"));
List<WebElement> trs=tbl.findElements(By.xpath("//tr[#class='tr-heading']"));
System.out.println(trs.size());
This below code returns single tr,
WebElement tbl=dr.findElement(By.xpath("//div[#class='large 20 columns']/table[1]/tbody"));
List<WebElement> trs1=tbl.findElements(By.cssSelector("tr[class='tr-heading']"));
System.out.println(trs1.size());
It seems Xpath is taking the whole div and returns two tr.Can you please tell me why this difference is and how do i have to do using xpath.
Use dot(.)as it Selects the current node
WebElement tbl=dr.findElement(By.xpath("//div[#class='large 20 columns']/table[1]/tbody"));
List<WebElement> trs=tbl.findElements(By.xpath(".//tr[#class='tr-heading']"));
System.out.println(trs.size());
//Now it will print out 1 as in the current node there is only one tr tagwith the specified class name
Related
I captured the testName by using driver.find_element_by_xpath("//td[contains(.,'%s')]" % test_name)
How can I capture class name based on testName?
I am trying to click on menu-button if it contains specific test name
<tr class="row-1">
<td>testName</td>
<td>testDes</td>
<td class="menu">
<div class="menu-button">
</div>
</td>
</tr>
This is one possible XPath expression :
//tr[td[contains(.,'testName')]]/td[#class='menu']/div[#class='menu-button']
Basically the above XPath locate tr element having child td value equals "testName", then return the corresponding div[#class='menu-button'] element.
How to retrieve the text 'caught jake' from the below code using selenium webdriver?
Am able to point to that text using the below xpath but am unable to print the text. :(
//*[#id='full-scorecard']/div[2]/div/table[1]/tbody/tr[3]/td[2]/child::text()
<div class="row">
<div class="large 20 columns">
<table class="batting-table innings" width="100%">
<tbody>
<tr class="tr-heading">
<tr>
<tr class="dismissal-detail" style="display: table-row;">
<td width="2%" />
<td colspan="8">
<b>12.6</b> caught Jake
<b>73/4</b>
<br/>
Using java you can retrieve from following lines:
String text = driver.findElement(By.xpath("//div[#class='large 20 columns']")).getText();
System.Out.print("Text= "+text);
Above line will return all inner tags string's of div element.
If you want to get only 12.6 text, then use below lines:
String text = driver.findElement(By.xpath("//table[#class='batting-table innings']/tr/tr/tr/td[2]/b")).getText();
System.Out.print("Text= "+text);
And be sure that there should be single element which has this xpath 'By.xpath("//table[#class='batting-table innings']/tr/tr/tr/td[2]/b")', otherwise you will get an exception.
depend on your language, you can use following xpath expression:
//div[#class='large 20 columns']/table/tbody/tr/tr/td[2]/child::text()
full Java code:
JavascriptExecutor js = (JavascriptExecutor) driver;
String jsx = "return document.evaluate(\"//*[#id='full-scorecard']/div[2]/div/table[1]/tbody/tr[3]/td[2]/child::text()\", document, null, XPathResult.STRING_TYPE, null).stringValue;";
String objList = js.executeScript(jsx);
System.out.println( (String) objList);
I wanted to delete a particular row in a table in UI using the associate id... there is delete image for each row. My logic was
1)To find the associate id and get its xpath and then equalise the row value of the xpath with that of the delete image and click on the respective image.
2) To find the associate id and get its rowspan and then give the rowspan in the xpath of the delete image as shown below. (but i dono how to get the rowspan of it).
Kindly help on it.
WebElement elem = driver.findElement(By.xpath("//*[contains(.,'EDUAR ALEX')]"));
if (elem == null)
System.out.println("The text is not found on the page!");
else {
System.out.println("The text is present!!");
}
int rowCount=driver.findElements(By.xpath("//*[contains(.,'EDUAR ALEX')]")).size();
System.out.println(rowCount);
driver.findElement(By.xpath("html/body/form/div[4]/div[2]/div[1]/div[2]/div[2]/table/tbody/tr["+rowCount+"]/td[6]/a[2]/img")).click();
Thanks in advance!!!
<tr class="borderLeft">
<td align="center" class="col2">389206</td>
<td align="center" class="col3">EDUAR ALEX</td>
<td align="center" class="col4">Administrator,GIT,Report</td>
<td align="center" class="col5">410561</td>
<td align="center" class="col6">Apr 7 2014 11:47AM</td>
<td align="center" id="389206" class="col12"><a class="Edit_User" title="edit" id="edit"><img width="16" height="16" id="389206" class="EditClass" alt="pdf" title="Administrator,GIT,Report" src="../StyleSheet/Images/page_white_edit.png"> </a><a id="389206" title="delete" class="delete"><img width="16" height="16" id="389206" class="DeleteClass" alt="Edit" src="../StyleSheet/Images/delete.png"></a></td></tr>
<tr class="altRow">
<td class="col2" align="center">360396</td>
<td class="col3" align="center">BERNIC JIM</td>
<td class="col4" align="center">Report</td>
<td class="col5" align="center">360396</td>
<td class="col6" align="center">Apr 11 2014 6:50PM</td>
<td class="col12" align="center" id="360396"><a id="edit" title="edit" class="Edit_User"><img src="../StyleSheet/Images/page_white_edit.png" width="16" height="16" title="Report" alt="pdf" class="EditClass" id="360396"> </a><a class="delete" title="delete" id="360396"><img src="../StyleSheet/Images/delete.png" width="16" height="16" alt="Edit" class="DeleteClass" id="360396"></a></td>
I think you want to delete particular row which has anchor and its successive sibling is image and you want to delete that row using that id of anchor tag can you please provide html code of any one of that row so that it would be more easier, I have done some think like it earlier using code like
WebElement element=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#name='"+Name+"']["+(rowCount+1)+"]/succeeding-sibling::*")));
element.click();
Please post html
List<WebElement> links = driver.findElements(By.tagName("td"));
Iterator<WebElement> itr = links.iterator();
while(itr.hasNext()){
String DataToDelete = itr.next().getText();
if(DataToDelete.equals("EDUAR ALEX")){
// you can find the xpath using this code but to reach and click the desired delete image u can use below code
String xpath = driver.findElement(By.name(DataToDelete)).getAttribute("xpath");
//or below code uses id to reach the desired anchor tag and then it's succeeding sibling which is image in ur case and click on that image
String id = driver.findElement(By.name(DataToDelete)).getAttribute("id");
List<WebElement> ElementID=driver.findElements(By.xpath("//a[#id='"+id+"']"));
Iterator<WebElement> anchoritr=ElementID.iterator();
while(anchoritr.hasNext()){
WebElement AllAtributes=anchoritr.next();
String GetTitle=AllAtributes.getAttribute("title").toString();
if(GetTitle.equalsIgnoreCase("delete")){
WebElement delete=AllAtributes.findElement(By.xpath("//a[#id='"+id+"']/succeeding-sibling::*"));
delete.click();
}
}
}
}
Please let me know if it works or not.
I want to get tooltip information enabling on JavaScript. I have tried with below code but its shown null every time.
Java Code:
Actions action = new Actions(driver);
WebElement element = driver.findElement(By.xpath("//*[#id='content']/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div[1]/table/tbody/tr[1]/td[2]/span/div/a"));
action.moveToElement(element).build().perform();
System.out.println(driver.findElement(By.xpath("//*[#id='content']/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div[1]/table/tbody/tr[1]/td[2]/span/div/a")).getAttribute("data-original-title"));
HTML Code is:
<tr class="single" name="Tasks_59c777d9-8d16-694a-7307-52caad36d751">
<td>
</td>
<td data-type="custom_task_name">
<span class="list" sfuuid="3840" data-original-title="">
<div class="ellipsis_inline" data-original-title="">
Task Testing
<br/>
Toney Harber
</div>
</span>
</td>
I have tried with span tag but this also shows null.
I am providing you 2 options try both.
Option 1
Please try below. It will return you a list. Loop through it. If there is only one span then you can get the element and then get the attribute value.
driver.findElements(By.xpath("//span[#data-original-title]"))
Option 2
Instead of
driver.findElement(By.xpath("//*[#id='content']/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div[1]/table/tbody/tr[1]/td[2]/span/div/a"))
use findElements
driver.findElements(By.xpath("//*[#id='content']/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div[1]/table/tbody/tr[1]/td[2]/span/div/a"))
It will return a list of web elements. Loop thorough it you might get more than one element.
I have a datagrid that I need to retrieve the value next to the item that I can identify dynamically.
What the scenario is trying to do:
This is HTML form a Roles Datagrid which contains a Role Name and a count of permissions. An admin would click on a role, a dialog box would pop up and the admin would update the role and save. The Role count would update. My script checks that the number increments and decrements based on the scenario.
What I'm trying to do is come up with a script that will search based on role name in the "data-rk" attribute and then collect the data in the adjacent permissions count column.
Here is the table:
<tbody id="rolesForm:rolesTable_data" class="ui-datatable-data ui-widget-content">
<tr class="ui-widget-content ui-datatable-even" aria-selected="false" role="row"
data-rk="Role1" data-ri="0">
<td role="gridcell">Role1</td>
<td role="gridcell">
<span id="rolesForm:rolesTable:0:permissions">3</span>
</td>
</tr>
<tr class="ui-widget-content ui-datatable-odd ui-state-hover" aria-
selected="false" role="row" data-rk="Role2" data-ri="1">
<td role="gridcell">Role2</td>
<td role="gridcell">
<span id="rolesForm:rolesTable:1:permissions">38</span>
</td>
</tr>
Any suggestion on how to retrieve that data?
The following code will help you get the count for the specific data-rk attribute that you want. For this example, I am searching for a tr element having data-rk attribute "Role1"
First find the table body element by it's ID attribute and then get the string in the permissions count column from within this element as follows:
WebElement table = driver.findElementById("rolesForm:rolesTable_data");
String pCount = table.findElement(By.xpath("/tr[#data-rk=\"Role1\"]/td[2]")).getText();
// The returned object is a string. If you like, you could convert it to an integer
int count = Integer.parseInt(pCount);
Please note that this code will only return the first tr element that satisfies the condition. If your table is going to have multiple tr elements that satisfy this condition, then you will have to create a list of tr elements and then iterate over them to get the count. You can get the list as follows:
WebElement table = driver.findElementById("rolesForm:rolesTable_data");
List<WebElement> rows = table.findElements(By.xpath("/tr[#data-rk=\"Role1\"]);
// Iterate over rows to get each count
for (WebElement row : rows) {
String pCount = row.findElement(By.xpath("/td[2]")).getText();
// Do whatever you want with pCount
}