WebUI.click with xpath from webelement

Hello,

I’m trying to get elements from the UI and then take some actions over them. What I am trying to do is that, I have 3 buttons and I want to click on each of them.

These buttons are dynamic, they can be 2, 3 , … So I want to make it dynamically. For that I use this:

List<WebElements> elements = DriverFactory.getWebDriver().findElements(By.xpath("//div[@class='sc-bdVaJa sc-hwwEjo kIFSBP sc-gZMcBi hgNaev']"))

//def myList = []

WebUI.delay(1)
for(WebElements elem in elements) {
	WebUI.click(elem)
	WebUI.delay(2)
	NavigationService.navigateToPage1()
	WebUI.delay(2)
	NavigationService.navigateToPage2()
	WebUI.delay(2)
}

So, I expect that WebUI.click will work so that, it will click on the first taken element, then second and so. But I am getting error when it tries to click on elem.
image

I’ve also used TestObject to click an element.

//String xpath = "//div[@class='sc-bdVaJa sc-hwwEjo kIFSBP sc-gZMcBi hgNaev']"
//TestObject to = new TestObject("objectName")
//to.addProperty("xpath", ConditionType.EQUALS, xpath)

but if I will use WebUI.click(to), it will click 3 times on the same object.

Is this possible to make so, how I want ?

Instead of:

WebUI.click(elem)

try:

elem.click()

hmm, it worked only at the first time, it has clicked went to another page and when it came to the same page, I got an error:
Hello,

I’m trying to get elements from the UI and then take some actions over them. What I am trying to do is that, I have
image

Yes, I was afraid of that. Whenever you click something that redirects you (like a link, in your case), any elements you’ve located become “stale”. Your list of links becomes stale in this case. Try copying the part where you get the list of links into your loop as well:

for(WebElements elem in elements) {
	elem.click()
	WebUI.delay(2)
	NavigationService.navigateToPage1()
	WebUI.delay(2)
	NavigationService.navigateToPage2()
	WebUI.delay(2)
    elements = DriverFactory.getWebDriver().findElements(By.xpath("//div[@class='sc-bdVaJa sc-hwwEjo kIFSBP sc-gZMcBi hgNaev']"))
}
2 Likes

I did something, but I don’t know if this is correct way to achieve my goal.

I made so:
Well, as I searched for, that stale element reference exception is when some object does not appear on the DOM. So I used that :

List<WebElements> element = DriverFactory.getWebDriver().findElements(By.xpath("//div[@class='sc-bdVaJa sc-hwwEjo kIFSBP sc-gZMcBi hgNaev']"))

WebUI.delay(1)

for(int i = 0; i < element.size(); i++) {
	List<WebElements> elementAgain = DriverFactory.getWebDriver().findElements(By.xpath("//div[@class='sc-bdVaJa sc-hwwEjo kIFSBP sc-gZMcBi hgNaev']"))
	elementAgain[i].click()
	WebUI.delay(2)
	NavigationService.navigateToPage1()
	WebUI.delay(2)
	NavigationService.navigateToPage2()
	WebUI.delay(2)
}

Well, I declare that xpath what I want outside for statement. In the for statement, I just saying to it, that iteration should be done how much size does that elements have. And in for statement, I declare same xpath again on each iteration.

I really don’t know if this is correct. Well, it works but…

I’ve posted almost same answer, yes ? :smiley:

EDIT: I don’t know why, but what you’ve posted, I am getting same error, but when i use for statement it works.

1 Like