简体   繁体   中英

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(...)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM