硒webdriver找不到正确数量的元素 [英] selenium webdriver does not find the correct number of elements

查看:90
本文介绍了硒webdriver找不到正确数量的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初,我在这里发布我的问题:

Initially, I posted my question here:

从动态网站中提取内容使用Java库

然后,在阅读并应用以下问题中的信息后:

Then, after reading and applying the info from the question below:

Selenium Webdriver:未显示正确的Li元素

我安装了一个selenium chrome驱动程序(版本ChromeDriver 74.0.3729.6),我的chrome浏览器的版本为74.0.3729.169.硒WebDriver Java对象仍然无法正确找到网页上的元素数量,尽管我模拟了向下滚动,并且驱动程序打开的chrome浏览器确实正确显示了20个元素的总数.

I installed a selenium chrome driver (Version ChromeDriver 74.0.3729.6), my chrome browser has version 74.0.3729.169. The selenium WebDriver java object is still unable to correctly find the number of elements on my web-page, altough I simulated a scroll-down and the chrome browser that the driver opened did correctly show the total number of 20 elments.

    import java.util.List;

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;

     public class ImmoweltBot {

    public static final String URL2 = "https://www.immowelt.at/liste/wien-2-leopoldstadt/wohnungen/mieten?sort=price&cp=2";


    public static void main(String[] args) throws Exception {
        System.setProperty("webdriver.chrome.driver", "C:\\Temp\\chromedriver.exe");

        WebDriver webDriver = new ChromeDriver();
        webDriver.get(URL2);
        WebDriverWait wait = new WebDriverWait(webDriver, 15);
        By searchResults = By.xpath("//*[contains(@class, 'listitem clear relative js-listitem')]");

        JavascriptExecutor js = (JavascriptExecutor)webDriver;
        webDriver.manage().window().maximize();
        js.executeScript("window.scrollBy(0,1000)");

        wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(searchResults, 4));
        List<WebElement> elemnts = webDriver.findElements(searchResults);
        System.out.println(elemnts.size());
    }

}

我的网页:

https://www .immowelt.at/liste/wien-2-leopoldstadt/wohnungen/mieten?sort = price& cp = 2

任何帮助将不胜感激.谢谢!

Any help will be appreciated. Thank you!

推荐答案

感谢这个问题,它是如此具有挑战性.所以这是我的解决方案. 这是js可以平滑滚动直到向下.

Thanks for this question, it was so challenging. So Here is my solution. This is js to smooth scroll until down.

(async function() {
function sleep() {
    return new Promise(resolve => setTimeout(resolve, 500))
};
var height;
do {
    height = document.body.scrollHeight;
    window.scrollTo({
        "behavior": "smooth",
        "left": 0,
        "top": document.body.scrollHeight
    });
    await sleep()
} while (height != document.body.scrollHeight)})();

我使用了异步函数,因为chomedriver.executeScript()希望异步函数使用'await'语句.

I used async function because chomedriver.executeScript() wants async function to use 'await' statement.

String scrollWhileScrollsJS = "(async function(){function sleep(){return new Promise(resolve=>setTimeout(resolve,500))};var height;do{height=document.body.scrollHeight;window.scrollTo({\"behavior\":\"smooth\",\"left\":0,\"top\":document.body.scrollHeight});await sleep()}while(height!=document.body.scrollHeight)})();";
( (ChromeDriver) webDriver ).executeScript( scrollWhileScrollsJS );

当然,我们需要流利的等待.为此,我发现只有在页面底部时,"scrollY"才等于"document.body.scrollHeight-innerHeight".

And of course we need fluent wait. For this, i found that 'scrollY' will be equals to 'document.body.scrollHeight-innerHeight' only while we on the bottom of the page.

new FluentWait<>( webDriver ).withTimeout( Duration.ofSeconds( 10 ) )
                             .pollingEvery( Duration.ofMillis( 500 ) )
                             .until( result -> ( (ChromeDriver) webDriver ).executeScript( "return scrollY" ).equals( ( (ChromeDriver) webDriver ).executeScript( "return document.body.scrollHeight-innerHeight" ) ) );

因此,您可以使用此代码滚动页面,等待页面滚动到末尾,然后获取不知道应该有多少元素的元素.

As a result, you can use this code to scroll the page, wait for it is scrolled to the end and get elements without knowledge of how many it should be.

PS:请不要……我的意思是,真的,请不要在自动化测试中使用while(true).

PS: please, do not... i mean, really, DO NOT use while(true) in your automation tests.

这篇关于硒webdriver找不到正确数量的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆