【问题标题】:How to find the size of the page numbers in a website如何在网站中查找页码的大小
【发布时间】:2018-05-30 09:43:54
【问题描述】:

我正在尝试查找页面中存在的页码的大小。

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);

我试过上面的代码,但它打印的大小为“0” 有什么建议 ??

【问题讨论】:

  • 我已经为此添加了答案。请检查代码并让我知道您的反馈。

标签: java selenium automation


【解决方案1】:

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

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

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);

【讨论】:

  • @sara 好消息。很高兴知道它解决了您的问题
【解决方案2】:

只有在页面中打印page numbers present的大小才可以使用下面的代码块:

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 版本和最新的GeckoDriver 二进制文件,所以请尝试添加 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());

【讨论】:

  • 看起来你和用户的代码是一样的。但是他的size = 0。这就是为什么我添加了将元素拉到屏幕中心的代码。
  • 但在我这边,在将视图添加到中心代码后,它可以与 OP 的代码一起正常工作。
  • @MahmudRiad 不,OP 在&lt;a&gt; 标签之前使用了/ 而不是//。他至少应该得到1个后裔。但是使用// 他会得到11
  • @DebanjanB 我仍然得到 OP - 0 ??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-27
  • 2023-03-31
  • 2016-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多