温馨提示:本文翻译自stackoverflow.com,查看原文请点击:java - Element not found when executing findElement in Selenium
java selenium captcha invisible-recaptcha findelement

java - 在Selenium中执行findElement时找不到元素

发布于 2020-03-29 21:51:21

我正在尝试填写接连出现的多个表格,所有表格都会迅速填充而不会出现错误,因为我确保添加

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&amp;ssl=1&amp;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);

结果:与上一页相同。

Firefox页面来源:https : //pastebin.com/Kv15V2SK

屏幕截图的Firefox Inspect元素:http : //prntscr.com/qvi6hc

这很奇怪,因为页面源与表单不同!

查看更多

提问者
DM Malkawi
被浏览
140
Ahmet Aziz Beşli 2020-02-01 11:31

我找不到时间解决您的问题。如果您想自己做,请在Google上搜索“ Shadow Root,Selenium”,以前我曾遇到过此类错误。我所知道的是,您不能直接到达影子根内部的元素,这就是为什么您没有将源代码包含在其中。

您需要做的是逐步了解元素:

你必须扩大影子根,

这是影子根扩展功能:

public static WebElement expand_shadow_element(WebElement element)
    {
        WebElement shadow_root = (WebElement)((JavascriptExecutor)driver).executeScript("return arguments[0].shadowRoot", element);
        return shadow_root;
    }

你可以想象这个功能像

.switchTo.frame()

目前..

经过一些研究,您将了解影子根源。

我希望我能解决问题..

尝试使用此功能,如果不能,稍后我会帮助您。祝好运。