简体   繁体   English

硒webdriver找不到正确数量的元素

[英]selenium webdriver does not find the correct number of elements

Initially, I posted my question here: 最初,我在这里发布我的问题:

Extracting content from a dynamic web site using a Java Library 使用Java库从动态网站中提取内容

Then, after reading and applying the info from the question below: 然后,在阅读并应用以下问题的信息后:

Selenium Webdriver : not displaying the correct Li elements Selenium Webdriver:未显示正确的Li元素

I installed a selenium chrome driver (Version ChromeDriver 74.0.3729.6), my chrome browser has version 74.0.3729.169. 我安装了硒chrome驱动程序(版本ChromeDriver 74.0.3729.6),我的chrome浏览器的版本为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. 硒WebDriver Java对象仍然无法正确找到网页上的元素数量,尽管我模拟了向下滚动,并且驱动程序打开的chrome浏览器正确显示了20个元素的总数。

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

}

My web-page: 我的网页:

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

Any help will be appreciated. 任何帮助将不胜感激。 Thank you! 谢谢!

It is a bit tricky.You have to use infinite loop to check the elements size() and scroll down to page once it reach 20 it will jumps out of the loop. 这有点棘手,您必须使用无限循环来检查元素的size()并向下滚动到页面,直到它达到20就会跳出循环。

WebDriver driver = new ChromeDriver();
driver.get("https://www.immowelt.at/liste/wien-2-leopoldstadt/wohnungen/mieten?sort=price&cp=2");
WebDriverWait wait = new WebDriverWait(driver, 15);

while(true){

  List<WebElement> elemnts=wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//div[contains(@class, 'listitem clear relative js-listitem')]")));                               

   driver.findElement(By.tagName("body")).sendKeys(Keys.DOWN);

   if (elemnts.size()==20)
       {
          System.out.println(elemnts.size());   
          break;
       }               

        } 

Thanks for this question, it was so challenging. 感谢这个问题,它是如此具有挑战性。 So Here is my solution. 所以这是我的解决方案。 This is js to smooth scroll until down. 这是js可以平滑滚动直到向下。

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

I used async function because chomedriver.executeScript() wants async function to use 'await' statement. 我使用了异步函数,因为chomedriver.executeScript()希望异步函数使用'await'语句。

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

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. 为此,我发现只有在页面底部时,“ scrollY”才等于“ document.body.scrollHeight-innerHeight”。

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: please, do not... i mean, really, DO NOT use while(true) in your automation tests. PS:请不要……我的意思是说真的,请勿在自动化测试中使用while(true)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM