org.openqa.selenium.InvalidArgumentException:未知错误:不支持的协议

时间:2017-05-26 12:56:18

标签: java arrays selenium selenium-webdriver webdriver

我在Arraylist的网页中添加了所有链接,然后逐个点击所有网址。

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),第一个链接稍后会抛出错误:

  

线程“main”中的异常   org.openqa.selenium.InvalidArgumentException:未知错误:   不支持的协议         (会议信息:chrome = 58.0.3029.110)         (驱动信息:chromedriver = 2.26.436362(5476ec6bf7ccbada1734a0cdec7d570bb042aa30),platform = Windows NT   6.1.7601 SP1 x86_64)(警告:服务器未提供任何堆栈跟踪信息)       命令持续时间或超时:131毫秒       构建信息:版本:'未知',修订版:'3169782',时间:'2016-09-29 10:24:50 -0700'       系统信息:主机:'ETPUN-LT009',ip:'192.168.2.193',os.name:'Windows 7',os.arch:'amd64',os.version:'6.1',java.version:   '1.8.0_111'       驱动程序信息:org.openqa.selenium.chrome.ChromeDriver       功能[{applicationCacheEnabled = false,rotate = false,mobileEmulationEnabled = false,networkConnectionEnabled = false,   铬= {chromedriverVersion = 2.26.436362   (5476ec6bf7ccbada1734a0cdec7d570bb042aa30),   userDataDir = C:\ scoped_dir12784_32532},takesHeapSnapshot = true,   pageLoadStrategy = normal,databaseEnabled = false,handlesAlerts = true,   hasTouchScreen = false,版本= 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 =}]       会话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)         在org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)         在org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:164)         在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:636)         在org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:323)         在Practices.Redirectionlinked1.main(Redirectionlinked1.java:99)

我的配置是: -

  

Chrome - 版本58.0.3029.110(64位)

     

Geckodriver-v0.16.1-Win64的

     

Windows -7

     

Java - 1.8.1

3 个答案:

答案 0 :(得分:1)

这可能是因为您网站中的链接看起来像#resources/123.img,这些链接不是完整的URL并且触发get会导致异常。您应该检查以确保网址有效。这可以通过使用link.startsWith("http://") || link.startsWith("https://")

进行比较来减少

还有其他地方,你测试也会失败。

  1. finalList被声明为List并返回。必须将此更改为List,并且应使用值value填充。这是因为我们有一个for循环,你在其中调用driver.get(newLink),它将重置finalList中的所有WebElement对象,因为它们是先前找到的并给出异常。

  2. img代码没有href。而是使用&#39; src&#39;

  3. 以下是所有这些更改后的代码。请注意,可能还有其他条件来检查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();
                    }
                }
            }
        }
    

答案 1 :(得分:0)

我最近收到错误org.openqa.selenium.InvalidArgumentException:无效参数org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:282)

在我的情况下,这是因为我使用的是Cucumber 5.5.0,而当我尝试将完整的url作为变量传递给Scenario Outline数据表时,Cucumber感到困惑并导致Selenium出错。

答案 2 :(得分:0)

升级您的Selenium Web驱动程序。这是Webdriver和您的浏览器版本的兼容性问题。 我使用最新版本的chrome网络驱动程序更新了pom文件,该文件可以正常工作。将chrome Web驱动程序版本升级到3.9.1及更高版本也可以解决该问题。

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-chrome-driver</artifactId>
    <version>4.0.0-alpha-5</version>
</dependency>