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"));
Related
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...
I have problem to automate this drop down using selenium web driver using Java
This is the link - Go to 5th drop down named as Github users (fetch. js)
I am not able to enter the data into search field.
I am using send keys after perform click but it throws an exception like this " element is not interact able"
Steps I follow
driver.findElement(By.xpath("xapth")).click
drop down opens with no options because it is searchable and options are coming dynamically after entering key word into the search field.
driver.findElement(By.xpath("xapth")).sendkeys("Test");
Sendkeys are not working in this case because of drop down closed when perform send keys action.
<div class="Select-placeholder">Select...</div>
Below is the code which is working.
Please do optimize the code by removing thread.Sleep and putting some meaningful waits as per your requirement.
driver.Navigate().GoToUrl("https://jedwatson.github.io/react-select/");
IWebElement element1 = driver.FindElement(By.XPath("//span[#id='react-select-6--value']"));
IWebElement element2 = driver.FindElement(By.XPath("//span[#id='react-select-6--value']/div[2]/input[1]")) ;
element1.Click();
Thread.Sleep(2000);
element2.SendKeys("Test");
Thread.Sleep(1000);
element2.SendKeys(Keys.Tab);
Please note that element2 gets activated once you click on element1.
Try the following code:
public void typeAndSelect() {
WebElement searchBox = driver.findElement(By.xpath("//div[#class='section'][5]//div[#class='Select-control']"));
searchBox.click();
WebElement inputField = driver.findElement(By.xpath("//div[#class='section'][5]//input[#role='combobox']"));
inputField.clear();
String searchWord = "test";
inputField.sendKeys(searchWord);
WebElement selectDropdown = driver.findElement(By.xpath("//div[#class='Select-menu-outer']//div[#role='option'][text()='" + searchWord +"']"));
// wait for search results.
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(selectDropdown)).click();
}
Correct the following xpath part
"//div[#class='section'][5]"
to your implementation of the dropdown
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());
}
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.
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.