How to check all elements in a <ul> list using Selenium WebDriver?
Is it possible to skip all li elements of the <ul> </ul> element. Let's say I have an unknown number of li elements, so one way to scroll through them would be to impose a for loop with the maximum number of li s, say 100, and impose try and catch .
try{ for (int i=0; i<100; i++) { driver.findElement(By.xpath("//div[@id='...']/ul/li[i]")); } } catch {...} However, it does not recognize index i ? How can I recognize him?
Is there a better way?
+7
Buras
source share4 answers
Webdriver has a findElements API that can be used for this purpose.
List<WebElement> allElements = driver.findElements(By.xpath("//div[@id='...']/ul/li")); for (WebElement element: allElements) { System.out.println(element.getText()); } +22
vidit
source shareYour source code may work if you infer index i from a string as follows:
try { for (int i=0; i<100; i++) { driver.findElement(By.xpath("//div[@id='...']/ul/li["+i+"]")); } } catch {...} 0
Dee
source share List<WebElement> allElements = driver.findElements(By.xpath("//div[@id='...']/ul/li")); int s=allElements.size(); for(int i=1;i<=s;i++){ allElements = driver.findElements(By.xpath("//div[@id='...']/ul/li")); allElements.get(i).click(); } Use this
0
Ramesh bala
source shareYour goal is to get all li within ul. So he must find ul first, and then all li in that ul. You can use the following Selenium Java code:
WebElement ul_element = driver.findElement(By.xpath("//ul[@class='list-unstyled']")); List<WebElement> li_All = ul_element.findElements(By.tagName("li")); System.out.println(li_All.size()); for(int i = 0; i < li_All.size(); i++){ System.out.println(li_All.get(i).getText()); } //OR for(WebElement element : li_All){ System.out.println(element.getText()); } 0
Ripon al wasim
source share