Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

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

question from:https://stackoverflow.com/questions/65851452/element-not-clickable-when-using-findelement-on-another-webelement

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...