Java 如何查找网站中页码的大小

Java 如何查找网站中页码的大小,java,selenium,automation,Java,Selenium,Automation,我试图找到页面中出现的页码的大小 d.get("http://www.moneycontrol.com/stocks/advice/display_more.php"); java.util.List<WebElement> list = d.findElements(By.xpath("//div[@class='gray2_11']/a")); int u=list.size(); System.out.println(u); d.get

我试图找到页面中出现的页码的大小

d.get("http://www.moneycontrol.com/stocks/advice/display_more.php");    
java.util.List<WebElement> list = d.findElements(By.xpath("//div[@class='gray2_11']/a"));
        int u=list.size();
        System.out.println(u);
d.get(“http://www.moneycontrol.com/stocks/advice/display_more.php");    
java.util.List List=d.findElements(By.xpath(“//div[@class='gray2_11']]/a”);
int u=list.size();
系统输出打印LN(u);
我尝试了上面的代码,但它将大小打印为“0”
有什么建议吗

您必须将元素恢复到可见模式

然后调用list元素。我在这里添加完整的代码

driver.get("http://www.moneycontrol.com/stocks/advice/display_more.php");   


String scrollElementIntoMiddle = "var viewPortHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);"
            + "var elementTop = arguments[0].getBoundingClientRect().top;"
            + "window.scrollBy(0, elementTop-(viewPortHeight/2));";


((JavascriptExecutor) driver).executeScript(scrollElementIntoMiddle, driver.findElement(By.className("nextBtn")));
    //((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", ele);
try {
     Thread.sleep(500);
} catch (InterruptedException e) {
}



java.util.List<WebElement> list = driver.findElements(By.xpath("//div[@class='gray2_11']/a"));
int u=list.size();
System.out.println(u);
driver.get(“http://www.moneycontrol.com/stocks/advice/display_more.php");   
String scrollElementIntoMiddle=“var viewPortHeight=Math.max(document.documentElement.clientHeight,window.innerHeight | | 0);”
+“var elementTop=参数[0]。getBoundingClientRect().top;”
+“window.scrollBy(0,elementTop-(viewPortHeight/2));”;
((JavascriptExecutor)driver.executeScript(scrollElementIntoMiddle,driver.findElement(By.className(“nextBtn”)));
//((JavascriptExecutor)driver.executeScript(“参数[0]。ScrollingToView(true);”,ele);
试一试{
睡眠(500);
}捕捉(中断异常e){
}
java.util.List List=driver.findElements(By.xpath(“//div[@class='gray2_11']/a”);
int u=list.size();
系统输出打印LN(u);

仅在打印页面中出现的
页码的大小时,可以使用以下代码块:

d.get("http://www.moneycontrol.com/stocks/advice/display_more.php");    
System.out.println(d.findElements(By.xpath("//div[@class='gray2_11']//a")).size());
控制台输出:

11

更新: 您仍然获得
0
元素的原因是您尚未将浏览器升级到最新的
Firefox Quantum
版本和最新的
二进制,因此请尝试添加
WebDriverWait
如下:

System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
WebDriver d = new FirefoxDriver();
d.get("http://www.moneycontrol.com/stocks/advice/display_more.php");    
System.out.println(new WebDriverWait(d, 20).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//div[@class='gray2_11']//a"))).size());

我补充了一个答案。请检查代码并让我知道您的反馈。看起来您和用户的代码是相同的。但他这边的尺寸越来越大。这就是为什么我添加了代码来将元素拉到屏幕中央。但在我这边,在将视图添加到中心代码后,它与OP s代码配合得很好。@MahmudRiad没有,OP使用了
/
而不是
/
之前的
/
标记。他至少应该有一个体面的人。但是使用
/
他将得到
11
@DebanjanB我仍然得到OP-0???@sara好消息。很高兴知道它解决了你的问题