簡體   English   中英

Java Selenium卡在“ driver.get”上

[英]Java Selenium stuck on 'driver.get'

我有一個代碼應該循環訪問文件中的鏈接,但是在打開第一個鏈接后卡住了,什么也不做。 我有一個類似的代碼,可以正常工作。 我能想到的唯一區別是鏈接,但是我不明白為什么鏈接會導致代碼卡住

這是初始驅動程序設置:

    System.setProperty("webdriver.gecko.driver",
            "/home/ xxx /Documents/Selenium/geckodriver");
    WebDriver driver = new FirefoxDriver();

這是卡住的代碼:

    try {
        Scanner input = new Scanner(new File("unsplashPicLink.txt"));
        System.out.println("file readed");

        while (input.hasNextLine()) {
            linkFromFile = input.nextLine();
            // this line gets printed
             System.out.println(linkFromFile);

            // opens the web page with the picture
            driver.get(linkFromFile);
            // the following text does not get printed
            System.out.println("Show this text");

            // code here for further processing

        }
    } catch (FileNotFoundException ex) {
        System.out.println("file not found");
    }

如您在上面看到的,代碼被卡在' driver.get(linkFromFile) ;'上,顯示該網站並停留在該網站上。

我要拉的鏈接如下:

https://images.unsplash.com/photo-1437650128481-845e6e35bd75?dpr=1&auto=format&crop=entropy&fit=crop&w=1199&h=800&q=80&cs=tinysrgb

我嘗試添加:

linkFromFile= linkFromFile.substring(0, linkFromFile.indexOf("?"));
linkFromFile= linkFromFile.replace("https", "http");

這樣鏈接看起來像:

http://images.unsplash.com/photo-1437650128481-845e6e35bd75

但這沒有任何區別。 該頁面需要一段時間才能加載(例如5-10秒)。

我還嘗試添加:

            try {
                driver.get(linkFromFile);
            } catch (Exception e) {
                System.out.println("Error");
            }

但這並沒有任何改變。

有什么想法為什么代碼會卡住嗎? 通過卡住,我的意思是打開鏈接之后,沒有其他事情發生。 我希望收到“顯示此文本”消息並打開下一個鏈接。

例如,在同一個類中,我有以下代碼循環遍歷文件的鏈接,似乎沒有問題

        while (input.hasNextLine()) {
            linkFromFile = input.nextLine();

            driver.get(linkFromFile);

            try {
                List<WebElement> no = driver
                        .findElements(By.tagName("img"));

            // code here for further processing

            } catch (Exception e) {
                System.out.println("error " + e);
            }
        }

我為代碼工作感到困惑,而沒有類似的代碼。

謝謝

如果有幫助,這就是我初始化chrome webdriver的方式,也許這是一個錯誤? 只是猜測:

    //I declare the following globally just under 'public  class"insernamehere"'
    private WebDriver driver;

    //I then declare the following in my @Before part
//of the test so that it initializes before every test.
    System.setProperty("webdriver.chrome.driver", "C:/Users/ComputerName/Desktop/chromedriver2.23/chromedriver.exe");
    driver = new ChromeDriver();

讓我知道這是否有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM