我是Selenium和WebDriver的新手.
我有这个HTML:
<input id="undefined-undefined-Jobsubject-5546" type="text" value="" data-test="testing-job-subject" style="padding: 0px; position: relative; width: 100%; border: medium none; outline: medium none; background-color: transparent; color: rgb(255, 255, 255); cursor: initial; font: inherit; height: 100%; Box-sizing: border-Box; margin-top: 14px;"/>
driver.findElement(By.xpath("//input[@data-test='testing-job-subject']"));
但错误是:
org.openqa.selenium.NoSuchElementException: Unable to locate element: //input[@data-test=’testing-job-subject’]
我也试过这个:
driver.findElement(By.xpath("//*[starts-with(@id,'undefined-undefined-Jobsubject')]"));
因为id中的数字已生成,所以我不能使用By.id(….),但是会出现相同的错误.
是的,我在代码中有超时,因此元素在页面上.
问题出在哪儿?谢谢
解决方法:
如果您将NoSuchElementException作为提供的异常,则可能有以下原因:-
>可能是当您要查找元素时,该元素将不会出现在DOM上,因此您应该实现webdriverwait以等待元素可见,如下所示:-
webdriverwait wait = new webdriverwait(driver, 10);
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[data-test='testing-job-subject']")));
>该元素可能位于任何框架或iframe中.如果是这样,则需要在找到以下元素之前切换该框架或iframe:-
webdriverwait wait = new webdriverwait(driver, 10);
//Find frame or iframe and switch
wait.until(ExpectedConditions.frametoBeAvailableAndSwitchToIt("your frame id or name"));
//Now find the element
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[data-test='testing-job-subject']")));
//Once all your stuff done with this frame need to switch back to default
driver.switchTo().defaultContent();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。