Iterating on the table using Selenium - java

I don't understand why x-path doesn't working in table. I'm trying to iter table from the website I found xpath but Eclipse told me:
"Unable to find element with xpath".
Should I find xpath in tables another way?
Website URL: https://www.oferty.net/mieszkania/szukaj?ps%5Blocation%5D%5Btype%5D=1&ps%5Btype%5D=1&ps%5Btransaction%5D=1&ps%5Blocation%5D%5Btext%5D=dolno%C5%9Bl%C4%85skie
I try to read one offer for example:
String urlwyniki ="https://www.oferty.net/mieszkania/szukaj?ps%5Blocation%5D%5Btype%5D=1&ps%5Btype%5D=1&ps%5Btransaction%5D=1&ps%5Blocation%5D%5Btext%5D=dolno%C5%9Bl%C4%85skie";
driver.get(urlwyniki);
String xpathResult = "//html/body/div[2]/div[4]/div[2]/div[2]/div/div[2]/div[5]/table/tbody/tr[5]/td";
String sCellValue = driver.findElement(By.xpath(xpathResult)).getText();
System.out.print(sCellValue);
What did I do wrong?

Find the below code to iterate all cell value in the table. It may help you.
String urlwyniki ="https://www.oferty.net/mieszkania/szukaj?ps%5Blocation%5D%5Btype%5D=1&ps%5Btype%5D=1&ps%5Btransaction%5D=1&ps%5Blocation%5D%5Btext%5D=dolno%C5%9Bl%C4%85skie";
driver.get(urlwyniki);
String xpathResult = "//table/tbody/tr/td";
List<WebElement> rows = driver.findElements(By.xpath(xpathResult));
List<WebElement> cols=new ArrayList<WebElement>();
for(int i=0;i<rows.size();i++){
System.out.print("Row value: "+i);
cols=rows.get(i).findElements(By.tagName("td"));
for(WebElement col:cols)
System.out.print("cell value "+col.getText());
}

Related

Trying to get the text out of a List of webelements with Selenium WebDriver

In my code, I try to find all elements with a specific name, then try taking each elements' descendant and get its title, link and price. The price I'm having issues with because it sticks to the price tag of the first element from the WebElements list.
List<WebElement> autos = driver.findElements(By.xpath("//section[contains(#class ,'ui-search-results')]/ol/li//a[#class = 'ui-search-result__content ui-search-link']"));
for(WebElement auto : autos) {
String model = auto.getAttribute("title");
String page = auto.getAttribute("href");
String price = auto.findElement(By.xpath("//span[#class = 'price-tag-fraction']")).getText();
System.out.println(model + page + price);
}
Console is printing model and page just fine but the price is always the same one. I already tested the site and there is a price-tag-fraction per element.
When you use XPath and want to start searching from a specific element, you need to add a . to the start of the XPath. In your case
"//span[#class = 'price-tag-fraction']"
becomes
".//span[#class = 'price-tag-fraction']"
Your updated code
List<WebElement> autos = driver.findElements(By.xpath("//section[contains(#class ,'ui-search-results')]/ol/li//a[#class = 'ui-search-result__content ui-search-link']"));
for(WebElement auto : autos) {
String model = auto.getAttribute("title");
String page = auto.getAttribute("href");
String price = auto.findElement(By.xpath(".//span[#class = 'price-tag-fraction']")).getText();
System.out.println("Model: %s, Page: %s, Price: %s".formatted(model, page, price));
}
NOTE: I changed your print statement to make it easier to read. You could also write these to a CSV file and then open them later in Excel, etc. as a table.

how to find sub tagname by using By.tagName in selenium?

i am struggling with find sub tag name
i want to find "ankit patel" name from using below code
enter image description here
i am writing my code for as below
List<WebElement> rows= driver.findElements(By.id("ui-id-2") ); //Printing the size of the rows
List<WebElement> lirowsx = rows.findElements(By.tagName("li"));
System.out.print(lirowsx .size() +"size ");
int s=lirowsx.size();
when i am trying to access "ankit patel" using "li" tag then it returns and error message
like
Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable
(Session info: chrome=80.0.3987.132)
i have also tried with xpath but didn't get result.
can you please guide me how can i solve this ?
You can find the element by first finding it by xpath and then fetching its text.
You can do it like:
WebElement element = driver.findElement(By.xpath("//li[#class='ui-menu-item']//strong"));
// Fetching the text
String text = element.getText();
If the li element id is unique then
WebElement element = driver.findElement(By.xpath("//li[#id='ui-id-11']//strong"));
or
WebElement element = driver.findElement(By.xpath("//li[#id='ui-id-11']/div/div/strong"));
and to get the text
String text = element.getText();
Feel free to post any more errors you see...

Why my code snippet always returns/prints the first record data from the list?

I am trying to retrieve the records of users from the grid using the below code from OrangeHRM demo site with demo credentials. However, It always returns/prints the first record values i.e. Admin. Could you tell me where I did wrong?
URL: http://opensource.demo.orangehrmlive.com/
User: Admin
Password: admin
Users Grid data: Admin > User Management> User page
Objective: Read user names data
Code:
WebElement element;
List<WebElement> usersList = driver.findElements(By.xpath("//*[#id='resultTable']/tbody/tr"));
for (int i = 0; i < usersList.size(); i++) {
element = usersList.get(i);
System.out.println(i+":"+element.findElement(By.xpath("//td[2]/a")).getText());
}
I know, we can achieve it by usng xpath "//*[#id='resultTable']/tbody/tr/td[2]/a". But, I'd like to select checkbox based on the given name and what's my mistake.
Thanks for your help in advance.
Try below solutions
System.out.println(i+":"+element.findElement(By.xpath("td[2]/a")).getText());
System.out.println(i+":"+element.findElement(By.xpath("decendent:td[2]/a")).getText());
System.out.println(i+":"+element.findElement(By.xpath(".td[2]/a")).getText());
You can find it more convenient way like below.
WebElement table = driver.findElement(By.id("resultTable"));
List<String> list = new ArrayList<String>();
List<WebElement> oddNames = table.findElements(By.className("odd"));
for(WebElement elem : oddNames){
System.out.println(elem.findElement(By.tagName("a")).getText());
String name = elem.findElement(By.tagName("a")).getText();
list.add(name);
}
List<WebElement> evenNames = table.findElements(By.className("even"));
for(WebElement elem : evenNames){
System.out.println(elem.findElement(By.tagName("a")).getText());
String name = elem.findElement(By.tagName("a")).getText();
list.add(name);
}
use the below code you will get list of usernames
List<WebElement> _Name=driver.findElements(By.xpath("//table[#id='resultTable']/tbody/tr"));
for(int j=0;j<_Name.size();j++)
{
String str="//table[#id='resultTable']/tbody/tr["; System.out.println(driver.findElement(By.xpath(str+j+"]/td[2]")).getText());
}
First get all tr elements and keep them on a WebElement list. Then get the user link element using tagName a in the loop. And finally use getText() to retrieve each user name.
You can use like below:
List<WebElement> usersList = driver.findElements(By.xpath("//*[#id='resultTable']/tbody/tr"));
for (WebElement userElem : usersList) {
String username = userElem.findElement(By.tagName("a")).getText();
System.out.println(username);
}
I think, it is more convenient than other solutions as the solution not using any tr td index. You know, different browser behaves differently. so index may be changed sometimes for different browser.

Reading a table using Selenium

I am facing difficulties when trying to read the following table using selenium.
HTML
The locator is as below
#FindBy(css = "table.table-bordered.table-striped")
private WebElement storesTable;
List<WebElement> storeRows = storesTable.findElements(By.tagName("/tbody/tr"));
The count of rows is 0 when the code is run. Any help is greatly appreciated.
The reason for this is: you are giving invalid TagName as "/tbody/tr" plus you need to change you storesTable as below because tr is present under tbody :
#FindBy(css = "table.table-bordered.table-striped > tbody")
private WebElement storesTable;
and after changing your tagName:
List<WebElement> storeRows = storesTable.findElements(By.tagName("tr"));
You are providing xpath in place of tagName.
If you want to use tagname below is the statement
List<WebElement> storeRows = storesTable.findElements(By.tagName("tr"));
Or else if you want to use xpath below is the statement.
List<WebElement> storeRows = storesTable.findElements(By.xpath(".//tbody/tr"));

how to use List<WebElement> webdriver

I am creating an automatic test for some webs and I'm using WebDriver, TestNG and code that is written in Java. On the page is shown register of categories, in parentheses is number of auctions and i need to get this number as variable.
I use this code
By bycss =By.cssSelector(".list.list-categories>li:first-child");
WebElement number1_1_vse = driver.findElement(bycss );
String text_vse1_1 = number1_1_vse.getText();
but I get only first number and i need to get all. Someone advised me that I should use List. But when i use it, i get only
[[[[[[[FirefoxDriver: firefox on WINDOWS (7e6e0d0f-5cbb-4e48-992f-26d743a321a5)] -> css selector: .list.list-categories>li:first-child]] -> xpath: ..]] -> xpath: .//*], [[[[[[FirefoxDriver: firefox on WINDOWS (7e6e0d0f-5cbb-4e48-992f-.....
code:
By bycss2 =By.cssSelector(".list.list-categories>li:first-child");
WebElement number1_1_vse2 = driver.findElement(bycss2 );
WebElement parent1 = number1_1_vse2.findElement(By.xpath(".."));
List<WebElement> childs1 = parent1.findElements(By.xpath(".//*"));
System.out.println(childs1);
link to the website
screenshot -> image with the number
can anyone advise me please?
Try the following code:
//...
By mySelector = By.xpath("/html/body/div[1]/div/section/div/div[2]/form[1]/div/ul/li");
List<WebElement> myElements = driver.findElements(mySelector);
for(WebElement e : myElements) {
System.out.println(e.getText());
}
It will returns with the whole content of the <li> tags, like:
<a class="extra">Vše</a> (950)</li>
But you can easily get the number now from it, for example by using split() and/or substring().
Try with below logic
driver.get("http://www.labmultis.info/jpecka.portal-exdrazby/index.php?c1=2&a=s&aa=&ta=1");
List<WebElement> allElements=driver.findElements(By.cssSelector(".list.list-categories li"));
for(WebElement ele :allElements) {
System.out.println("Name + Number===>"+ele.getText());
String s=ele.getText();
s=s.substring(s.indexOf("(")+1, s.indexOf(")"));
System.out.println("Number==>"+s);
}
====Output======
Name + Number===>Vše (950)
Number==>950
Name + Number===>Byty (181)
Number==>181
Name + Number===>Domy (512)
Number==>512
Name + Number===>Pozemky (172)
Number==>172
Name + Number===>Chaty (28)
Number==>28
Name + Number===>Zemědělské objekty (5)
Number==>5
Name + Number===>Komerční objekty (30)
Number==>30
Name + Number===>Ostatní (22)
Number==>22
List<WebElement> myElements = driver.findElements(By.xpath("some/path//a"));
System.out.println("Size of List: "+myElements.size());
for(WebElement e : myElements)
{
System.out.print("Text within the Anchor tab"+e.getText()+"\t");
System.out.println("Anchor: "+e.getAttribute("href"));
}
//NOTE: "//a" will give you all the anchors there on after the point your XPATH has reached.

Categories