通过开发人员工具查看时,Selenium WebDriver无法找到页面源中不存在但HTML中存在的元素 [英] Selenium WebDriver not able to find elements which are not present in the Page Source but present in HTML when seen through Developer Tools

查看:144
本文介绍了通过开发人员工具查看时,Selenium WebDriver无法找到页面源中不存在但HTML中存在的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium Web驱动程序HtmlUnitDriver来搜索网页上的元素. 我只能搜索页面源"中可见的那些元素. 但是,我可以使用Internet Explorer开发人员工具(F12)看到这些元素的详细信息. 当我使用Selenium使用这些详细信息(id/name/XPath)时,它会抛出org.openqa.selenium.NoSuchElementException. 我写的代码是:

I am using Selenium Web Driver HtmlUnitDriver for searching elements on a web page. I am able to search only those elements that are visible in the Page Source. However I can see these elements details using the Internet Explorer Developer Tools (F12). When I use these details (id/name/XPath) using Selenium, it throws a org.openqa.selenium.NoSuchElementException. The code I have written is :

public static void main(String[] args) {

        WebDriver driver = new HtmlUnitDriver(){
            @Override
            protected WebClient modifyWebClient(WebClient client) {

                DefaultCredentialsProvider credentialsProvider = new DefaultCredentialsProvider();
                credentialsProvider.addNTLMCredentials("username", "password", null, -1, "localhost", "domain");
                client.setCredentialsProvider(credentialsProvider);
                return client;
            }
        };

        driver.get("URL");

        System.out.println(driver.getPageSource());
        WebElement myDynamicElement = (new WebDriverWait(driver, 20)).until(ExpectedConditions.presenceOfElementLocated(By.id("ppm_timesheet")));

    }

页面资源

<?xml version="1.0" encoding="UTF-8"?>
    <html>
    <head>
    <meta name="application-name" content="CONTENT"/>
    <meta name="upk-namespace" content="en"/>
    <meta http-equiv="X-UA-Compatible" content="IE=8"/>
    <link rel="SHORTCUT ICON" href="ui/uitk/images/shortcut.ico"/>
    <title>
      Title
    </title>
    <link rel="stylesheet" href="ui/uitk/css/min.css"/>
    <script type="text/javascript" src="ui/ext/ext.min.js">
    </script>
    <script type="text/javascript">
    //<![CDATA[
    require( {baseUrl: "ui"},[ "uif/js/main.min" ] );
    //]]>
    </script>
  </head>
  <body>
    <div id="ppm">
    </div>
  </body>
</html>

如果我搜索元素"ppm",则表示页面源中存在该元素成功. 当我搜索元素"ppm_timesheet"时,出现以下异常,可能是因为页面源中不存在该元素. 但是,当我在Internet Explorer中按F12并在开发人员工具中选择此元素时,该元素将显示在HTML中:

If I search the element "ppm", it is successful which is present in page source. When I search the element "ppm_timesheet" I am getting following exception maybe because this element is not present in the page source. But when I press F12 in Internet Explorer and select this element in developer tools, this element is present there in HTML:

<button title="Time" class="class" id="ppm_timesheet" type="submit" jQuery171013988872804261454="13" alt="Time">

完整的例外是:

Exception in thread "main" org.openqa.selenium.TimeoutException: Timed out after 20 seconds
Build info: version: '2.8.0', revision: '14056', time: '2011-10-06 12:41:26'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1 build 7601 Service Pack 1', java.version: '1.6.0'
Driver info: driver.version: unknown
    at org.openqa.selenium.support.ui.FluentWait.timeoutException(FluentWait.java:220)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:188)
    at com.auto.Automation.main(Automation.java:32)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element with ID: ppm_timesheet
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.8.0', revision: '14056', time: '2011-10-06 12:41:26'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1 build 7601 Service Pack 1', java.version: '1.6.0'
Driver info: driver.version: HtmlUnitDriver
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementById(HtmlUnitDriver.java:685)
    at org.openqa.selenium.By$ById.findElement(By.java:210)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1222)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:969)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1219)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:396)
    at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:289)
    at org.openqa.selenium.support.ui.ExpectedConditions.access$0(ExpectedConditions.java:287)
    at org.openqa.selenium.support.ui.ExpectedConditions$3.apply(ExpectedConditions.java:86)
    at org.openqa.selenium.support.ui.ExpectedConditions$3.apply(ExpectedConditions.java:1)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:173)

我已阅读到某些元素是在客户端生成的,而在视图源中看不到.但这是否意味着我们根本无法访问不在View Source中的那些元素.

I have read that some elements are generated on the client side and not seen in view source. But does that imply that we cannot access those elements at all which are not in View Source.

我已经尝试等待元素加载30秒.但是我仍然遇到相同的错误.

I have already tried waiting for the element to load for 30 seconds. But still I am getting the same error.

请提供您的输入,因为我在Google上搜索了很多内容,但无法弄清这一点.

Please provide your inputs as I have googled a lot and not able to figure this out.

推荐答案

根据您的方案说明可能有一个原因 动态加载内容.
在这种情况下,硒代码运行速度更快,而浏览器端的html(java脚本/框架)代码需要花费一些时间才能从服务器加载动态数据. 因此,请使用waitdriver使selenium驱动程序等待,直到动态数据加载并追加到DOM

As per your Scenario description There can be one reason dynamically loading of content.
in this scenario selenium code runs faster while html(java script / framework) code at browser end take time to load dynamic data from server . so use waitdriver to make selenium driver wait until dynamic data loads and append to DOM

WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 10);
Wait.Until(ExpectedConditions.visibilityOfElementLocated(By.XPath(xpathOfElement));````    


这篇关于通过开发人员工具查看时,Selenium WebDriver无法找到页面源中不存在但HTML中存在的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆