Get texts from web dynamically

i want to get texts dynamically from web page. Well, I can easily get texts using xpath, but in my case those elements have same classes, but also these classes are used for many elements. So, I can’t just use xpath by getting only classes. Can I somehow get elements by using absolute xpath (nodes) ?

This is what I am trying to do:

List<WebElements> elements = DriverFactory.getWebDriver().findElements(By.xpath("//html[1]/body[1]/div[1]/div[1]/div[2]/div[2]/div[2]/div[1]/div[1]/div[3]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[2]/div[1]/div[2]/div[1]/div[1]/div[1]"))
		for(int i = 0; i < elements.size(); i++) {
			List<WebElements> elements = DriverFactory.getWebDriver().findElements(By.xpath("//html[1]/body[1]/div[1]/div[1]/div[2]/div[2]/div[2]/div[1]/div[1]/div[3]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[i]/div[1]/div[2]/div[1]/div[1]/div[1]"))
			   def text = elements.getText()
			   println text

Is is possible to get those elements dynamic with using absolute xpath ?

There are other ways of getting the element locators besides classes. Can you post your HTML so we can advise you?

1 Like


please, for your own safety, do not do this. There is always a safer way to get Xpath of element(s) rather than absolute path.

Please share your HTML code of elements you need and we may figure it out.

1 Like