2016-09-13 28 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

相关问题