我是硒的新手,正在努力解决一个看似简单的问题。
将Java 8与selenium webdriver和Chrome配合使用。
我的要求是访问此网站:
https://start.duckduckgo.com/
在搜索类型中:大象
现在,我需要使用selenium检索所有自动建议的值。
从浏览器控制台I
think
这是包含自动建议的必要HTML:
<div class="search__autocomplete" style="display: block;">
<div class="acp-wrap js-acp-wrap">
<div class="acp" data-index="0"><span class="t-normal">elephant toothpaste</span></div>
<div class="acp" data-index="1"><span class="t-normal">elephant toothpaste</span> experiment</div>
...
</div>
<div class="acp-footer is-hidden js-acp-footer">
<span class="acp-footer__instructions">Shortcuts to other sites to search off DuckDuckGo</span>
</div>
以下是我在没有运气的情况下尝试的代码:
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://start.duckduckgo.com/");
WebElement searchText = driver.findElement(By.name("q"));
searchText.sendKeys("elephant");
List<WebElement> searchList = new ArrayList<WebElement>();
// have tried various options : all of the below selectors return no results
//searchList = driver.findElements(By.xpath("//div[contains(@class, 'acp')]//span"));
//searchList = driver.findElements(By.xpath("//div[contains(@class,
'acp')]//span[contains(@class, 't-normal')]"));
//searchList = driver.findElements(By.cssSelector(".t-normal"));
//searchList = driver.findElements(By.cssSelector("t-normal"));
//searchList = driver.findElements(By.cssSelector(".acp > span"));
// this atleast returns a collection of 10 but not sure of its content
searchList = driver.findElements(By.xpath("//div[contains(@class, 'acp')]"));
if(null != searchList && searchList.size() > 0) {
for(int i = 0;i<searchList.size();i++) {
WebElement e = searchList.get(i);
System.out.println("element details are " + e.toString());
/** NoSuchElementException with below tried Xpath and by css
WebElement spanElement = e.findElement(By.className("t-normal"));
WebElement spanElement = e.findElement(By.className(".t-normal"));
WebElement spanElement = e.findElement(By.cssSelector(".t-normal"));
WebElement spanElement = e.findElement(By.cssSelector("t-normal"));
**/
WebElement spanElement = e.findElement(By.cssSelector("t-normal"));
System.out.println(e.getText());
}
}else {
System.out.println("<<<<< not able to locate >>>>>");
}
//div[contains(@class, 'acp-wrap js-acp-wrap')]//div[contains(@class, 'acp')]//span[contains(@class,'t-normal')]
那么,真的对如何从跨度内提取文本感到困惑吗?