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();
}
}
Related
I would like to find an element using a differect cssSelector / tag / ClassName amd them get it's xpath value ( to be more specific, I have a website where when a day changes, one of the classes change it's class) here is what do I meean:
<tr>
<td> 1.1.2019 </td>
<td> 2.1.2019 </td>
<td class="active"> 3.1.2019 </td>
<td> 4.1.2019 </td>
</tr>
<tr>
<td> </td>
<td> 10 </td>
<td> </td> #Here
<td> </td>
</tr>
I want to according to where is that "active class", click the table under it. ny idea how to do so ?
short version of what I want :
Find element using cssSelector
Get this element's Xpath <- the problem
click it using edited xpath
I want to GET XPATH OF LOCATED ELEMENT , not to locate it using Xpath
You can find the index by locating all the <td> elements in the first row and check wich one has the index
List<WebElement> columns = driver.findElements(By.xpath("//tr[td[#class='active']]/td")); # just an example, can be any other locator
int index = 0;
for (int i = 0 ; i < columns.getSize() ; i++) {
String attribute = columns.get(i).getAttribute("class")
if (attribute != null && attribute.equals("active")) {
index = i + 1;
}
}
I would like to find an element using a differect cssSelector / tag / ClassName amd them get it's xpath value ( to be more specific, I have a website where when a day changes, one of the classes change it's class) here is what do I meean:
<tr>
<td> 1.1.2019 </td>
<td> 2.1.2019 </td>
<td class="active"> 3.1.2019 </td>
<td> 4.1.2019 </td>
</tr>
<tr>
<td> </td>
<td> 10 </td>
<td> </td> #Here
<td> </td>
</tr>
I want to according to where is that "active class", click the table under it. ny idea how to do so ?
short version of what I want :
Find element using cssSelector
Get this element's Xpath <- the problem
click it using edited xpath
I want to GET XPATH OF LOCATED ELEMENT , not to locate it using Xpath
You can find the index by locating all the <td> elements in the first row and check wich one has the index
List<WebElement> columns = driver.findElements(By.xpath("//tr[td[#class='active']]/td")); # just an example, can be any other locator
int index = 0;
for (int i = 0 ; i < columns.getSize() ; i++) {
String attribute = columns.get(i).getAttribute("class")
if (attribute != null && attribute.equals("active")) {
index = i + 1;
}
}
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]"));
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.
I have an html with this form:
<table>
<tbody>
<tr>
<td class="t1"><img class="png" src="" alt="site1"></td>
<td class="t2 up">INFORMATION</td>
<td class="t2 down">INFORMATION</td>
<td class="t2 up mark">INFORMATION</td>
</tr>
<tr>
<td class="t1"><img class="png" src="" alt="site2"></td>
<td class="t2 down">INFORMATION</td>
<td class="t2 stable">INFORMATION</td>
<td class="t2 up">INFORMATION</td>
</tr>
.
.
.
</tbody>
</table>
and I want to extract or the value of href (/click/site1) or the value of alt (site1).
How can I do this using Jsoup??
thx
edit:
this is the code that I wrote:
for(Element table : doc.select("table"))
{
for(Element row : table.select("tr"))
{
System.out.print(table.attr("href").toString());
Elements column = row.select("td");
{
System.out.println(column.text());
}
}
System.out.println();
}
but this line System.out.print(table.attr("href").toString());doesn't print anything
This process is described in jsoup cookbook.
http://jsoup.org/cookbook/extracting-data/working-with-urls
Document doc = Jsoup.connect("http://jsoup.org").get();
Element link = doc.select("a").first();
String relHref = link.attr("href"); // == "/"
String absHref = link.attr("abs:href"); // "http://jsoup.org/"
In your question you try to get the attribute href from the table but the table doesn't have href attribute. Either you search for all a tags or you may select the td inside your row and then the link inside of that.
Did some coding add changed your example and added some code to only write the links.
for(Element table : doc.select("table")) {
for(Element row : table.select("tr")) {
Elements column = row.select("td");
Elements atag = column.get(0).select("a");
System.out.print(atag.get(0).attr("href").toString());
System.out.print(" ");
System.out.println(column.text());
}
System.out.println();
}
for(Element link : doc.select("a")) {
System.out.println(link.attr("href")); // == "/"
}