gpt4 book ai didi

java - Selenium WebDriver 无法找到页面源中不存在但通过开发人员工具查看时存在于 HTML 中的元素

转载 作者:行者123 更新时间:2023-11-29 08:48:27 28 4
gpt4 key购买 nike

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

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 中:

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

完整的异常(exception)是:

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 源中看不到。但这是否意味着我们根本无法访问那些不在 View Source 中的元素。

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

请提供您的意见,因为我在谷歌上搜索了很多但无法弄清楚。

最佳答案

您要查找的元素不在页面源代码中这一事实应该不是问题。 WebDriver API 是 introduced to explicitly add support for dynamic pages ,例如其中元素是通过 JavaScript 添加的。

然而,元素在 DOM 中是不够的,元素还需要可见。 WebDriver 意味着只允许用户也可以进行的交互,所以如果一个元素不可见,你应该例如无法通过 WebDriver 点击它。

关于java - Selenium WebDriver 无法找到页面源中不存在但通过开发人员工具查看时存在于 HTML 中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23949547/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com