WebElement.findElement method is finding element under WebDriver scope - java

I have the following HTML code on which I am trying to run my selenium test
<html>
<head></head>
<body>
<table id="Original">
<tr>
<td>Original-11</td>
<td>Original-12</td>
<td>Original-13</td>
</tr>
<tr>
<td>Original-21</td>
<td>Original-22</td>
<td>Original-23</td>
</tr>
<tr>
<td>Original-31</td>
<td>Original-32</td>
<td>Original-33</td>
</tr>
</table>
<br/><br/>
<table id="Duplicate">
<tr>
<td>Duplicate-11</td>
<td>Duplicate-12</td>
<td>Duplicate-13</td>
</tr>
<tr>
<td>Duplicate-21</td>
<td>Duplicate-22</td>
<td>Duplicate-23</td>
</tr>
<tr>
<td>Duplicate-31</td>
<td>Duplicate-32</td>
<td>Duplicate-33</td>
</tr>
</table>
</body>
</html>
The selenium java code looks like this:
public static void main(String[] args) throws Exception
{
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/src/test/drivers/chromedriver.exe");
WebDriver drv = new ChromeDriver();
drv.get("C:/Users/MYUserName/git/er_test/SeleniumTestHtml.html");
WebElement tableElement = drv.findElement(By.id("Duplicate"));
WebElement rowElement = tableElement.findElement(By.xpath("//tr[2]"));
WebElement cellElement = rowElement.findElement(By.xpath("//td[2]"));
System.out.println(rowElement.getText());
System.out.println(cellElement.getText());
drv.close();
drv.quit();
}
I am expecting a result as follows :
Duplicate-21 Duplicate-22 Duplicate-23
Duplicate-22
But I am getting this result :
Original-21 Original-22 Original-23
Original-12
Am I doing something wrong here ?

Your problem is that // makes you go back to search from top root element.
Which means
WebElement rowElement = tableElement.findElement(By.xpath("//tr[2]"));
Is as good as
WebElement rowElement = driver.findElement(By.xpath("//tr[2]"));
If you need to search only inside the current element then you should use
WebElement rowElement = tableElement.findElement(By.xpath(".//tr[2]"));

Related

Get data from table(html) except div tag by jsoup

I have html code:
<table width="100%" cellpadding="5" cellspacing="2" class="zebra">
<tr>
<td colspan="5">
<div class="paginator">
2
</div>
</td>
</tr>
<tr>
<td>some_value</td>
</tr>
<tr>
<td>some_value</td>
</tr>
<tr>
<td colspan="2">
<div class="paginator">
2
</div>
</td>
</tr>
</table>
I use Jsoup. How can I get all links except links in div tag?
I try to do something like this, but It doesn't work. Element contains all the links.
org.jsoup.nodes.Elements tableText = doc.select("table.zebra").not("tr td div.paginator");
for (org.jsoup.nodes.Element td : tableText.select("td a")) {
System.out.println(td.attr("href")); // http://some_link
....
}
You can use the below code..
Document html = Jsoup.parse(htmlStr);
for (Element e : html.getElementsByTag("a")) {
if (!"div".equalsIgnoreCase(e.parentNode().nodeName())) {
System.out.println(e.attr("href"));
}
}
Here I am checking that the parent node of the anchor element is not div. if it is not div I am printing the url.

Selenium, Java- finding table by class (without ID)

I have something like this:
<table class="table">
<thead>
<tr>
<th>Role</th>
<th>Description</th>
<th>Access</th>
<th>Grant</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="role in roles">
<td>{{role.role_name}}</td>
<td>{{role.role_description}}</td>
<td><input type="checkbox" ng-model="role.allow_access" ng-change="updateAllow(role)" ng-disabled="!(role.grantor_allow_grant == 'Y' || haveAllAdminRoles)" ng-true-value="'Y'" ng-false-value="'N'"></td>
<td><input type="checkbox" ng-model="role.allow_grant" ng-change="updateAllow(role)" ng-disabled="!(role.grantor_allow_grant == 'Y' || haveAllAdminRoles)" ng-true-value="'Y'" ng-false-value="'N'"></td>
</tr>
</tbody>
</table>
How I can find this table using WebDriver or WebConnector ? I tried sth like this:
WebElement table = driver.findElement(By.className("table"));
and it doesn't work, I received error:
org.openqa.selenium.NoSuchElementException: Unable to locate element:
Thanks for any help.
That might be due to timing issue. You can use explicit wait to wait for the table to load or to be visible
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement table = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("table")));
// or
WebElement table = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("table")));
May be when you are going to finding table, it could not be visible on the page due slow internet or other reason. To make sure table visible on the page try to find using WebDriverWait to wait until table visible as below :-
WebDriverwait wait = new WebDriverwait(driver, 10);
WebElement table = wait.until(Expectedconditions.visibilityOfElementLocated(By.className("table")));
Hope it helps..:)

HREF + TEXT with Jsoup

I've the following HTML Page:
</div><div id="page_content_list01" class="grid_12">
<h2><strong class="floatleft">TEXT1</strong></h2><br>
<table>
<tbody>
<tr>
<th class="no_width">
<p class="floatleft">Attachments:</p>
</th>
<td class="link_azure">
<a target="_blank" href="http://www.example.com">TEXT2</a><br/>
</td>
</tr>
</tbody>
</table><h2><strong class="floatleft">TEXT3</strong></h2><br>
<table>
<tbody>
<tr>
<th class="no_width">
<p class="floatleft">Atachments:</p>
</th>
<td class="link_azure">
<a target="_blank" href="http://www.example2.com">TEXT4</a><br/>
</td>
</tr>
</tbody>
</table><h2><strong class="floatleft">TEXT5</strong></h2><br>
<table>
<tbody>
<tr>
Actually I'm doing:
Elements rows = document.select("div#page_content_list01");
Now I to select "TEXT" and link. I wanna to make clickable link, so I'm using:
for (Element eleme : rows) {
Elements elements = eleme.select("a");
for (Element elem : elementi) {
String url = elem.attr("href");
String title = elem.text();
}
}
and I'm getting:
url = "http://www.example.com";
title = "TEXT2";
and it's ok, but in this way I can't read "TEXT1" and "TEXT3".
Can someone help me please?
I think you need to work on the selecors. First, your primary selector
Elements rows = document.select("div#page_content_list01");
will return with a list of ONE element only, since you actually select the div, not the tables or table rows. I would instead do this to get all relevant info:
Elements tables = document.select("div#page_content_list01>table");
for (Element table : tables){
Element h2 = table.previousElementSibling();
String titleStr = h2.text();
Element a = table.select("a").first();
String linkStr = a.attr("href");
}
Note that the Text in the h2 elements is on the same level as the table, not inside a common div. This is why I use the previous sibling notation. Also note that I wrote this out of my head and it is untested. You should get the idea though.

Getting Input tag id, value in webdriver using java

Html
<table id="tblRenewalList" class="adminlist dataTable" width="100%" cellspacing="1" cellpadding="1" border="1" style="margin-left: 0px; width: 100%;" aria-describedby="tblRenewalList_info">
<thead>
</thead>
<tbody role="alert" aria-live="polite" aria-relevant="all">
<tr class="odd">
<td class="alignCenter">
<input id="chkRenewal_868" class="chkPatent" type="checkbox" onclick="RenewalSelection(this)" companyid="33" value="868">
</td>
</tr>
</table>
with above Html i want to scrape the id, value
following are my java code, when i try with below code, its return empty values, please find the code
WebElement inputValues = driver.findElement(By
.xpath("//*[#id='tblRenewalList']/tbody/tr[1]/td[1]"));
String idValue = inputValues.getAttribute("id");
String ed2 = inputValues.getAttribute("value");
following are my expected output
id = chkRenewal_868
value = 868
The document isn't well-formed, i don't know if that matters for webdriver,
but XPath must be
//*[#id='tblRenewalList']/tbody/tr[1]/td[1]/input

Selenium Java How locating element issue?

I have some problems locating elements into a web page like this:
<tr id="filter100" style="...." idx=0
<td>
<div onclick=... style=...
<table dir = "fil">
<tbody>
<tr>
<td>
<img id="imgFil100_1" src="//path..."
<td>
<td>
<img id="imgFil100_2" src="//path..."
<td>
<td>
<img id="imgFil100_3" src="//path..."
<td>
And i have a lot of buttons in this way "filterXXX". How can i locate them and click on them.
i wrote this code
List<WebElement> lc = driver.findElements(By.cssSelector("table[id*='imgFil']"));
for (int i = 0; i <= lc.size(); i++) {
lc.get(i).click();}
BTW Sorry for my english.
List<WebElement> lc = driver.findElements(By.cssSelector("table[id*='filter']"));
for (WebElement row : lc) {
List<WebElement> images = row.findElements(By.tagName("img"));
for (WebElement image : images) {
image.click();
}
}

Categories

Resources