2016-09-13 26 views
0

我有一個代碼應該通過文件中的鏈接進行循環,但打開第一個鏈接後它會卡住,什麼都不做。我有一個類似的代碼,工作正常。我能想到的唯一的區別是鏈接,但我不明白爲什麼鏈接會導致代碼卡住Java Selenium卡在'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"); 
      } 

但它並沒有任何區別eihter。

任何想法爲什麼代碼卡住?通過卡住我的意思是,打開鏈接上面沒有其他事情發生。我期望的消息'顯示此文本',併爲下一個鏈接打開。

例如,在同一個班,我有以下的代碼,通過文件的鏈接循環,並似乎是沒有問題的

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

我一直困惑與工作代碼和一個類似不。

感謝

+0

你能後你是如何初始化的驅動程序? –

+1

你是什麼意思卡住?有什麼異常嗎?還需要共享驅動程序初始化代碼 –

+0

Jose&Saurabh,我編輯了代碼,添加了驅動程序初始化,我的意思是卡住了,這基本上是代碼無法完成。希望它是有道理的。 – Selrac

回答

0

如果有幫助,這就是我初始化我的鉻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(); 

讓我知道,如果這有助於

+0

謝謝MG97。我認爲驅動程序沒有問題(具有完全相同的設置),它與其他代碼 – Selrac

+0

Okey dokes一起工作,只是我添加了私人WebDriver驅動程序;宣言是我的不工作沒有,但酷我 – MG97

+0

我試過了,但它沒有任何區別。無論如何感謝您的評論 – Selrac

相關問題