我使用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"时,我得到以下例外,可能是因为此元素不存在于页面源中。但是当我在ie中按F12然后在开发人员工具中选择这个元素时,这个元素出现在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中的元素呢?
我已经尝试等待元素加载30秒。但我还是得到了相同的错误。
请提供您的输入,因为我已经谷歌了很多,无法弄清楚这个
您正在查找的元素不在页面源中,这应该不是问题。WebDriver API的引入是为了显式地添加对动态页面的支持,例如,在通过JavaScript添加元素的地方。
然而,元素在DOM中是不够的,元素还需要是可见的。WebDriver意味着只允许用户可以做的交互,所以如果一个元素不可见,你就不能通过WebDriver点击它。
根据您的场景描述,可能有一个原因动态加载的内容。在这种情况下,selenium代码运行得更快,而html(java脚本/框架)代码在浏览器端需要时间从服务器加载动态数据。所以使用waitdriver让selenium driver等待,直到动态数据加载并附加到DOM
WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 10);
Wait.Until(ExpectedConditions.visibilityOfElementLocated(By.XPath(xpathOfElement));````