我试图检查加载页面后是否通过Java中的Chrome Drive检查某个Webelement,但它不起作用
加载页面后,我正在尝试通过Java中的Chrome Drive检查某个Webelement。 即使该页面已加载并且元素存在,驱动程序也无法找到它,我得到了一个 NosuchelementException。 如果我让它运行更多的时间,它可能会做到这一点,但是使用我的代码逻辑,这是不可能的可单击,但是当我单击它时,什么也没发生,所以诸如“可见”之类的东西{如果那是一件事情}可能无法正常工作),然后把我带到正确的位置。
我的代码的一部分:
while (true) {
JavascriptExecutor js = (JavascriptExecutor) driver;
if (js.executeScript("return document.readyState").toString().equals("complete")) {
try {
if (driver.findElement(By.xpath("/html/body/div[1]/div/div[4]/div/div/div/div/div[1]/div")).isEnabled()) {
inProfilePictureTab = true;
toBreak = true;
break;
}
} catch (NoSuchElementException exception) {
warningLabelChanger("Profile picture isn't clickable, try something else");
toBreak = true;
break;
} catch (NullPointerException exception2) {
}
}
try {
Thread.sleep(Constants.SLEEP_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
我该如何解决?
I'm trying to Check if a certain WebElement is present through chrome drive in java after checking the page is loaded.
even though the page is loaded and the element is present the driver can't locate it and I get a
NoSuchElementException.
if I'll let it run more time it would probably make it but that's not possible with logic of my code because I'm checking if this element is present while the page is loaded to see if something is clickable (when I over it looks clickable but when I click it nothing happen so something like "isClickable" {if that's a thing} probably won't work) and gets me to the right place.
part of my code:
while (true) {
JavascriptExecutor js = (JavascriptExecutor) driver;
if (js.executeScript("return document.readyState").toString().equals("complete")) {
try {
if (driver.findElement(By.xpath("/html/body/div[1]/div/div[4]/div/div/div/div/div[1]/div")).isEnabled()) {
inProfilePictureTab = true;
toBreak = true;
break;
}
} catch (NoSuchElementException exception) {
warningLabelChanger("Profile picture isn't clickable, try something else");
toBreak = true;
break;
} catch (NullPointerException exception2) {
}
}
try {
Thread.sleep(Constants.SLEEP_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
How can I fix that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
评论中讨论的解决方案是添加等待。
创建驱动程序对象时添加一次:
The solution discussed in the comments was to add a wait.
Add this once when you create the driver object: