2017-08-12 67 views
0

无法检索列表中的所有元素 我试图使用“选择”,它是抛出错误信息。任何人都可以请告诉我,如何检索所有元素?目前的代码只能检索到5个元素。请查阅网站的我的代码硒WebDriver - 无法检索列表中的所有元素

继输出元素的列表

<h3 class="tout__title complex-link__target theme__target"> Almost Naked Animals </h3> 
<h3 class="tout__title complex-link__target theme__target"> Amazing Animal Births </h3> 
<h3 class="tout__title complex-link__target theme__target"> American Dad! </h3> 
<h3 class="tout__title complex-link__target theme__target"> An Audience with... </h3> 
<h3 class="tout__title complex-link__target theme__target"> Arsenal Pre-Season Friendlies </h3> 
<h3 class="tout__title complex-link__target theme__target"> Atlanta Plastic </h3> 
<h3 class="tout__title complex-link__target theme__target"> Audi Cup </h3> 
<h3 class="tout__title complex-link__target theme__target"> The Avatars </h3> 

我的代码是:

wait.until(visibilityOfElementLocated(By.cssSelector("h3[class='tout__title complex-link__target theme__target']"))); 

List<WebElement> pgm= alp.findElements(By.cssSelector("h3[class='tout__title complex-link__target theme__target']")); 

System.out.println(pgm.size()); 

for(WebElement e: pgm) 
{ 
    System.out.println("List of Shows "+e.getText()); 
} 

输出:

Size of the Element(Total) :8 
List of Shows Almost Naked Animals 
List of Shows Amazing Animal Births 
List of Shows American Dad! 
List of Shows An Audience with... 
List of Shows Arsenal Pre-Season Friendlies 
List of Shows Atlanta Plastic 
List of Shows 
List of Shows 
+0

它'pgm.size()=='8真的吗?如果是的话,我强烈怀疑你发布的html代码是正确的。你有链接到这个网站? –

+0

HI Ivan - 该网站是:https://www.itv.com/hub/shows。如果你有解决方案,请告诉我 – sri

回答

0

问题在于页面向下滚动时页面内容为lazy-loaded。所以,你的代码显示出现在屏幕上的前几个项目的文本。

您需要为其他要加载的元素添加页面滚动。例如。滚动到页面底部:

((JavaScriptExecutor) driver).executeScript("window.scrollTo(0,document.body.scrollHeight)"); 

页面后,滚动执行所有的元素文本相关的代码,它应该产生现在想要的结果