我正在设置测试以执行搜索,在搜索完成后,我想捕获显示“关于xxx结果(x.xx秒)”的结果行。我可以使用firefox webdriver来使用它,但是当我使用IE或Chrome驱动程序时它不起作用。我需要帮助确定我的代码有什么问题。以下是代码段:
public void runSearch(WebDriver driver) {
WebElement element = driver.findElement(By.name("search"));
element.sendKeys("Kearney");
element.sendKeys(Keys.RETURN);
String itext = driver.findElement( By.cssSelector("div#resInfo-0")).getText();
System.out.println("Search returned '" + itext + "'.");
}
以下是搜索完成后我的页面的html:
<head></head>
<body>
<header id="main_header_iphone"></header>
<div class="clear"></div>
<header id="main-header"></header>
<section>
<div class="content_nn">
<div class="subnav">
<!--
<div class="bannerBox">
<div class="btn_events"><…
-->
<div class="logo"></div>
<!--
START Search
-->
<div class="search_container">
<div id="cse" style="width:100%;">
<div class="gsc-control-cse gsc-control-cse-en">
<div class="gsc-control-wrapper-cse" dir="ltr">
<form class="gsc-search-box" accept-charset="utf-8"></form>
<div class="gsc-results-wrapper-nooverlay gsc-results-wrapper-visible">
<div class="gsc-tabsAreaInvisible"></div>
<div class="gsc-tabsAreaInvisible"></div>
<div class="gsc-above-wrapper-area">
<table class="gsc-above-wrapper-area-container" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gsc-result-info-container">
<div id="resInfo-0" class="gsc-result-info">
About 31 results (0.11 seconds)
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="gsc-adBlockNoHeight" style="height: 0px; font-weight: normal; text-align: center;"></div>
<div class="gsc-wrapper"></div>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript"></script>
<script type="text/javascript" src="http://www.google.com/uds/?file=search&v=1&hl=en"></script>
<link rel="stylesheet" type="text/css" href="http://www.google.com/uds/api/search/1.0/65b21018ad4df09e3eb5a21326b72d0b/default+en.css"></link>
<script type="text/javascript" src="http://www.google.com/uds/api/search/1.0/65b21018ad4df09e3eb5a21326b72d0b/default+en.I.js"></script>
<!--
END Search
-->
</div>
<div class="text_rural_h2" style="float:left; width:100%"></div>
<div class="index_page_heading" style="float:left;"></div>
<div class="letest_news2" style="float:left;"></div>
</div>
<footer id="footer"></footer>
</div>
</section>
<div id="topcontrol" style="position: fixed; bottom: 25px; right: 10px; opacity: 0; cursor: pointer;" title="Scroll Back to Top"></div>
<table class="gstl_50 gssb_c" cellspacing="0" cellpadding="0" style="width: 137px; display: none; top: 153px; position: absolute; left: 97px;"></table>
<div style="display:none"></div>
</body>
答案 0 :(得分:1)
有可能,搜索结果页面需要比WebDriver预期更多的加载时间 我建议等到下一页加载WebDriverWait方法。
element.sendKeys("Kearney");
element.sendKeys(Keys.RETURN);
// Wait!
WebDriverWait wait = new WebDriverWait(driver, 20);
By waitFor = By.cssSelector("div#resInfo-0");
WebElement lblSearchResults = wait.until(ExpectedConditions.ElementIsVisible(waitFor));
//
String itext = lblSearchResults.getText();
另请参阅此主题 - WebDriver: Advanced Usage
答案 1 :(得分:0)
对于IE和Chrome,您需要额外的驱动程序。从chrome driver开始,检查这是否解决了您的问题。