2017-05-26 86 views
0

我添加了網頁中的所有鏈接Arraylist,然後逐個打開所有網址。org.openqa.selenium.InvalidArgumentException:未知錯誤:不支持的協議

public class Redirectionlinked1 
{ 
    public static List findAllLinks(WebDriver driver) 
    { 
     List <WebElement> elementList = new ArrayList(); 
     elementList = driver.findElements(By.tagName("a")); 
     elementList.addAll(driver.findElements(By.tagName("img"))); 

     List finalList = new ArrayList(); 
     for(WebElement element : elementList) 
     { 
      if (element.getAttribute("href") != null) 
      { 
       finalList.add(element); 
      } 
     } 
     return finalList; 
    } 

    public static void main(String[] args) throws Exception 
    { 
     System.setProperty("webdriver.gecko.driver", "E:\\Softwares\\gecko\\geckodriver-v0.16.1-win64\\geckodriver.exe"); 
     System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe"); 

     WebDriver driver = new ChromeDriver(); 
     driver.get(" http://testsite.com"); 
     List <WebElement > allImages = findAllLinks(driver); 

     System.out.println("Total number of elements found " + allImages.size()); 
     driver = new ChromeDriver(); 
     URI uri =null; 
     for (WebElement element : allImages) { 
     if (!driver.getCurrentUrl().equals(element.getAttribute("href")) && driver.) 
     { 
      driver.manage().deleteAllCookies(); 
      driver.get(element.getAttribute("href")); 
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
      Thread.sleep(500); 
      System.out.println(element.getAttribute("href")); 
      uri = new URI(driver.getCurrentUrl()); 
      try 
      { 
       if(uri.getHost().equalsIgnoreCase("SpecificDomain.net")) 
       { 
        System.out.println(" Redirected URL-->> "+element.getAttribute("href")); 
       } 
      } 
      catch (Exception e) 
      { 
        e.printStackTrace(); 
      } 
     } 
    } 
} 

代碼工作正常(它會啓動URL在瀏覽器)的第一個鏈接後拋出一個錯誤:

Exception in thread "main" org.openqa.selenium.InvalidArgumentException: unknown error: unsupported protocol (Session info: chrome=58.0.3029.110) (Driver info: chromedriver=2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa30),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 131 milliseconds Build info: version: 'unknown', revision: '3169782', time: '2016-09-29 10:24:50 -0700' System info: host: 'ETPUN-LT009', ip: '192.168.2.193', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_111' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa30), userDataDir=C:\scoped_dir12784_32532}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=58.0.3029.110, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}] Session ID: df813868289a8f15f947ac620b3b1882 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:164) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:636) at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:323) at Practices.Redirectionlinked1.main(Redirectionlinked1.java:99)

我的配置是: -

Chrome - Version 58.0.3029.110 (64-bit)

Geckodriver-v0.16.1-win64

Windows -7

Java - 1.8.1

+1

只是一個猜測 - 你在get()方法的url中有一個前導空格。 – Grasshopper

+0

感謝您的回覆,我也嘗試過修剪,但沒有成功。 driver.get(element.getAttribute(的 「href」)修剪()); – user3302083

+0

我在說的是driver.get()方法 - driver.get(「http://testsite.com」); - 你有一個開始的空間。你得到的錯誤似乎提到這一行。嘗試刪除空間並檢查它是否有效 – Grasshopper

回答

1

這可能是因爲有你的網站鏈接與hre看起來像#,resources/123.img這是不完整的URLS和觸發獲取將導致異常。您應該進行檢查以確保網址是有效的。這可以通過使用比較使用link.startsWith("http://") || link.startsWith("https://")

還有其他地方也測試你會失敗。

  1. finalList被聲明爲List並被返回。這必須更改爲List,並且應該填入鏈接值。這是因爲我們有一個for循環,在其中調用driver.get(newLink),它會重置finalList中的所有WebElement對象,因爲它們在先前找到併發出異常。

  2. img標籤沒有href。而是使用'src'。

這裏是所有這些更改後的代碼。請注意,可能還有其他一些條件來檢查URL是否有效,我沒有在這裏列出。

public static List<String> findAllLinks(WebDriver driver) { 

     // Declare finalList as string. 
     List<String> finalList = new ArrayList<>(); 

     // Get the a tags 
     List<WebElement> elementList = driver.findElements(By.tagName("a")); 
     // get the img tags 
     elementList.addAll(driver.findElements(By.tagName("img"))); 

     for (WebElement element : elementList) { 
      // a tags have "href", img tags have src 
      String link = element.getTagName().equalsIgnoreCase("a") ? element.getAttribute("href") 
        : element.getAttribute("src"); 
      // Check if link is not null and whether is a valid link by checking 
      // starts with http or https 
      if (link != null && (link.startsWith("http://") || link.startsWith("https://"))) { 
       finalList.add(link); 
      } 
     } 
     return finalList; 
    } 

    public static void main(String[] args) throws Exception { 
     System.setProperty("webdriver.gecko.driver", 
       "E:\\Softwares\\gecko\\geckodriver-v0.16.1-win64\\geckodriver.exe"); 
     System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe"); 

     WebDriver driver = new ChromeDriver(); 
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
     driver.get("http://testsite.com"); 
     List<String> allLinks = findAllLinks(driver); 

     System.out.println("Total number of elements found " + allLinks.size()); 
     driver = new ChromeDriver(); 
     URI uri = null; 
     for (String link : allLinks) { 
      if (!driver.getCurrentUrl().equals(link)) { 
       driver.manage().deleteAllCookies(); 
       driver.get(link); 

       Thread.sleep(500); 

       System.out.println(link); 
       uri = new URI(driver.getCurrentUrl()); 
       try { 
        if (uri.getHost().equalsIgnoreCase("SpecificDomain.net")) { 
         System.out.println("Redirected URL-->> " + link); 
        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 
相關問題