【发布时间】:2020-01-31 01:50:57
【问题描述】:
我正在尝试填写多个表单,所有表单都可以快速填写,没有错误,因为我确保添加
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("")));
在新页面上做任何事情之前,我知道我在正确的页面上。
在最后一个表单上,我遇到了这个错误:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: //*[@id="formtovalidate"]/fieldset[1]/div/label/input For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
所以我通过截屏检查浏览器,浏览器在正确的页面上以正确的形式显示,我还检查了 xpath 值,甚至尝试了其他属性.. 似乎没有任何效果。
所以我继续打印出显示完全不同页面(不是上一页)的 PageSource,我还注意到在最终表单出现之前,此页面闪烁了一秒钟。
我也试过driver.navigate().refresh(),但没用。我一直在寻找和寻找,但什么都没有出现。我也换了浏览器,没用..
这是我正在尝试执行的方法:
private void method() {
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"formtovalidate\"]/fieldset[1]/div/label/input")));
driver.findElement(By.xpath("//*[@id=\"formtovalidate\"]/fieldset[1]/div/label/input")).sendKeys(email); }
更新
这是表单截图:
执行结果如下:
代码:
String body_text = driver.findElement(By.tagName("body")).getText();
System.out.println(body_text);
结果:表单但在文本中
代码:
String body_innerHTML = driver.findElement(By.tagName("body")).getAttribute("innerHTML");
System.out.println(body_innerHTML);
结果:不同的页面:(
<zendesk-ticketing-form base-url="https://www.runescape.com/a=870/c=K0aO9WO69EI" css-cachebust="129" sitekey="6Lcsv3oUAAAAAGFhlKrkRb029OHio098bbeyi_Hv" grecaptcha="" has-valid-session="true" weblogin-url="https://secure.runescape.com/m=weblogin/a=870/c=K0aO9WO69EI/loginform?mod=www&ssl=1&dest=zendesk/support-form?form=360000065898">
<div class="x-display-none ie-error-display" data-js-ie-error="">
<section class="c-article">
<div class="c-article__content">
<h1>Error: Unsupported Browser</h1>
<p>
We do not support your web browser. Please use a supported web browser by choosing one below.
<br>
<a href="https://www.mozilla.org/firefox/" target="_blank" rel="noopener">FireFox</a>
<br>
<a href="https://www.google.com/chrome/" target="_blank" rel="noopener">Chrome</a>
</p>
</div>
</section>
</div>
代码:
String pagesource = driver.getPageSource();
System.out.println(pagesource);
结果:与上一个相同..不同的页面..
火狐页面来源:https://pastebin.com/Kv15V2SK
Firefox Inspect Element 页面截图:http://prntscr.com/qvi6hc
这很奇怪,因为页面源与表单不同!
【问题讨论】:
-
首先,您可能需要提供 URL,甚至是整页源代码,以便我们查看。其次,请检查您的元素是否在 iframe 下。如果是这样,您必须将您的框架切换到该 iframe 才能找到您的元素。第三,如果它不在 iframe 下,则您的 xpath 可能是问题所在。您应该使用相对 xpath 而不是完整 xpath。
-
@MinhDao 谢谢你的回复,我已经在页面源代码中添加了..我右键单击表单并单击查看页面源..它很奇怪,因为它与表单本身不同..
-
好吧,您的网站已阻止您的浏览器,并返回错误消息:
Error: Unsupported Browser。在您的页面源代码中,检查第 93 行,它与您的屏幕截图看起来相同,但错误显示在第 99 行。 -
老实说,我从来没有遇到过这个错误。在我看来,您需要更新您的浏览器和驱动程序,并尝试其他浏览器以发现任何差异。希望这会有所帮助。
-
浏览器是最新更新的,我在 Firefox 上使用 gecko 驱动程序并在 Chrome 上使用 chromedriver79 进行了尝试
标签: java selenium captcha invisible-recaptcha findelement