Skip to content
Advertisement

Element not clickable when using FindElement on another WebElement

I have a weird problem:

When I locate an element via:

WebElement e1 = driver.findElement(By.xpath("//div1"));
WebElement e2 = e1.findElement(By.xpath("//.[@class='c2']"));
e2.click();

I can’t click e2, because: “Element < div class=”c2″ > could not be scrolled into view.”

BUT when I locate e2 via:

WebElement e2 = driver.findElement(By.xpath("//div1//.[@class='c2']"));
e2.click();

it works. (Also when checking e2.getLocation() the coordinates are different, and only correct in the second snippet).

I think Selenium doesn’t like the “//.”, because:

WebElement e1 = driver.findElement(By.xpath("//div1"));
WebElement e2 = e1.findElement(By.xpath("div[@class='c2']"));
e2.click();

also works.

Any idea how I can use the any (.) selector there? (.[@class='c2'] can not be found)

[EDIT] I already have e1 and therefore need to search on this element via e1.findElement(...)

Advertisement

Answer

WebElement e2 = e1.findElement(By.xpath("//.[@class='c2']"));

this is same as finding element as driver.findElement(By.xpath("//.[@class='c2']"));

because you haven’t given current node as reference

you should be using :

   e1.findElement(By.xpath(".//*[@class='c2']"));

See the ‘.’ in front of // , this means the current or reference node is the parent if you just use “//” instead of “//” it will search from the root

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement