This website has a funny structure, I actually managed to get what I want using selenium, but upon trying the second time, I cannot get the data I want. This is how it goes:-
1st Try (Correct information):- bank name list, bank account and balance
2nd Try (Wrong information):- bank transfer method, bank name list
I notice the div is shifted to different position suddenly. I'm using this xpath to extract the information I want:-
//div[12]/div[1]/ul[1]/li['.$x.']
Now I want it to extract base on the div id, so I came up with this xpath:-
//*[@id="idBnk_panel"]//div/ul/following::li
but it can't work properly to extract the information I need. Any idea how to grab the information from the li
tags based on this structure?
<div id="something_here">
<div>
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</div>
What I understand is you want to extract all 3 <li>
tag data . So here is the code for that -
List<WebElement> element = driver.findElements(By.xpath("//div[@id='something_here']/div/ul/li"));
for(WebElement iterate:element)
{
System.out.println(iterate.getText());
}
Here I 've used getText()
method, just get the text of <li>
tag . You can manipulate according your requirement what you want to do.
The xpath you are using is incorrect //*[@id="idBnk_panel"]//div/ul/following::li
The correct xpath as per the HTML you showed is //div[@id='something_here']/div/ul/li[index_number_as_required] or you can directly use //div[@id='something_here']//li[index_number_as_required]
and then use String text = driver.findElement(By.xpath(//div[@id='something_here']//li[index_number_as_required])).getText();
If the xpath is changing time to time due to some dynamic data display, since the "id" of a div is available, you can use below code also.
List<WebElement> mainLabels = new ArrayList<WebElement>();
mainLabels = driver.findElement(By.id("something_here")).findElements(By.tagName("li"));
//mainLabels list contains all the li elements
//Then you can access the texts either this way
mainLabels.get(0).getText();//This will give you the text that is in first li
mainLabels.get(1).getText();//This will give you the text that is in second li
//OR this way
for (int i = 0; i < mainLabels.size(); i++) {
System.out.println(mainLabels.get(i).getText());
}