java - 为什么 org.openqa.selenium.firefox.FirefoxDriver.navigate().to() 在前三个循环后不等待?

标签 java selenium

当我运行以下程序时...

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.By;
import java.util.*;

class GoogleSearch {

    static WebDriver driver = new FirefoxDriver();
    static ArrayList<String> hitUrls = new ArrayList<String>();

    public static void main(String[] args) throws InterruptedException {

        try {
            driver.get("http://www.google.co.uk/");
            submitSearch("selenium");
            loopHits();
        }

        finally {
            driver.close();
        }
    }

    static void submitSearch(String search) throws InterruptedException {
        WebElement searchBox = driver.findElement(By.name("q"));
        searchBox.sendKeys(search);
        searchBox.submit();
        Thread.sleep(1000);
    }

    static void loopHits() {
        List<WebElement> hits = driver.findElements(By.xpath("//h3/a"));

        for (WebElement element: hits) {
            hitUrls.add(element.getAttribute("href"));
        }

        long time1 = System.currentTimeMillis();

        for (String url: hitUrls) {
            System.out.println(url);
            driver.navigate().to(url);
            System.out.println(System.currentTimeMillis() - time1);
        }
    }
}

我将以下内容打印到控制台:

http://www.seleniumhq.org/
1136
https://ods.od.nih.gov/factsheets/Selenium-HealthProfessional/
3476
http://www.webmd.com/vitamins-and-supplements/lifestyle-guide-11/supplement-guide-selenium
6468
https://en.wikipedia.org/wiki/Selenium
6515
http://www.whfoods.com/genpage.php?dbid=95&tname=nutrient
6550
http://www.dailymail.co.uk/health/article-5009/How-diseases-selenium-beat.html
6574
http://www.webmd.boots.com/vitamins-and-minerals/selenium
6592
https://github.com/SeleniumHQ/selenium
6623
http://lpi.oregonstate.edu/mic/minerals/selenium
6645
https://seleniumhq.wordpress.com/
6671

为什么程序要等待前三页加载,而不等待其余页面加载?

最佳答案

驱动程序没有等待,因为您没有给它等待的理由。

要让它等待每个页面加载,您需要模拟一些操作(例如在页面上查找某些内容)

这可以通过询问每个页面的来源来完成:

例如,您可以在 loopHits() 方法中获取页面源代码:

static void loopHits()
{
    List<WebElement> hits = driver.findElements(By.xpath("//h3/a"));

    for (WebElement element : hits)
    {
        hitUrls.add(element.getAttribute("href"));
    }

    long time1 = System.currentTimeMillis();

    for (String url : hitUrls)
    {
        System.out.println(url);
        driver.navigate().to(url);

        // ask for the page source...
        driver.getPageSource();

        System.out.println(System.currentTimeMillis() - time1);
    }
}

在我的机器上运行,我可以看到时间差异如下所示:

http://www.seleniumhq.org/
1573
https://ods.od.nih.gov/factsheets/Selenium-HealthProfessional/
5173
http://www.webmd.com/vitamins-and-supplements/lifestyle-guide-11/supplement-guide-selenium
10824
https://en.wikipedia.org/wiki/Selenium
12613
http://www.whfoods.com/genpage.php?dbid=95&tname=nutrient
14239
http://www.webmd.boots.com/vitamins-and-minerals/selenium
16065
http://www.dailymail.co.uk/health/article-5009/How-diseases-selenium-beat.html
23928
https://github.com/SeleniumHQ/selenium
26697
https://seleniumhq.wordpress.com/
29079
http://lpi.oregonstate.edu/mic/minerals/selenium
33409
https://www.google.co.uk/aclk?sa=l&ai=CCSnbNaVEV4e-DZKDpgO6hoyoDc279rVE57yLrG_K9PgECAMQAWDJBqABlfnw9QPIAQGqBCVP0IoUF49U6i-W-LSbFn4urqTiQ8cVwVGqxLuw9CLUSmLBJaUzgAfTho8KkAcDqAemvhvYBwE&sig=AOD64_3aOSd8gM4Vt4iAkp8jHXbgzAv7JQ&clui=0&q=&ved=0ahUKEwiB5_mOs_PMAhUDeFIKHTtiDG0Q0QwIfA&adurl=http://clickserve.dartsearch.net/link/click%3Flid%3D43700001249650557%26ds_s_kwgid%3D58700000142442718%26%26ds_e_adid%3D29839647511%26ds_e_matchtype%3Dsearch%26ds_e_device%3Dc%26ds_e_network%3Dg%26%26ds_url_v%3D2%26ds_dest_url%3Dhttp://www.puritan.com/selenium-078%3Fscid%3D6831%26cmp%3Dgoo-_-MineralSelenium-_-selenium
41033
http://www.puritan.com/selenium-078?scid=6831&cmp=goo-_-MineralSelenium-_-selenium100mg
43442

关于java - 为什么 org.openqa.selenium.firefox.FirefoxDriver.navigate().to() 在前三个循环后不等待?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37421620/

相关文章:

selenium - 在 Katalon Studio 中计算项目、行、用户等

python - 使用 selenium 时出现错误 : Unable to move the cache: Access is denied,(python)

python - 使用自定义值在 Selenium 中定位元素

java - Node java : Install error: "fatal error LNK1181 cannot open input file DelayImp.lib"

java - 有关 JAX-WS Web 服务的详细信息

java - 在 JFrame Java 上在图像上方追踪一条线

java - 使用最小堆实现 Dijkstra 算法但失败

java - Selenium WebDriver waitForText()

javascript - 通过唯一子项查找父元素,但单击另一个子项

java - 使用指向单个方法的按钮区分单击了android按钮数组中的哪个android按钮