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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44202217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:00:45  来源:igfitidea点击:

org.openqa.selenium.InvalidArgumentException: unknown error: unsupported protocol

javaarraysseleniumselenium-webdriverwebdriver

提问by user3302083

I added all the links from web-page in Arraylistand then hit all the URLs one by one.

我添加了网页中的所有链接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();
            }
        }
   }
}

Code works as expected (it launches the URL in browser) for first link later throws an error :

代码按预期工作(它在浏览器中启动 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)

线程“main” org.openqa.selenium.InvalidArgumentException 中的异常:未知错误:不支持的协议(会话信息:chrome=58.0.3029.110)(驱动程序信息:chromedriver=2.26.436362(5476ec6bf7ccbada1734a0cbb04061a0cbb04061a0cbb7061a0.0.3029.110) 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, rotatable=false,mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa30), userDataDir=C:\scoped_dir12784_32532}, hasAlpagesSnapert=false, normal database handle=false, hasAlpagesSnapertLoadStrategy=false 58.0.3029.110,平台=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) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206) at 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.get(RemoteWebDriver.get) :323) 在 Practices.Redirectionlinked1.main(Redirectionlinked1.java:99)206) 在 org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:164) 在 org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:636) 在 org.openqa.selenium.remote.Remote .get(RemoteWebDriver.java:323) 在 Practices.Redirectionlinked1.main(Redirectionlinked1.java:99)206) 在 org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:164) 在 org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:636) 在 org.openqa.selenium.remote.Remote .get(RemoteWebDriver.java:323) 在 Practices.Redirectionlinked1.main(Redirectionlinked1.java:99)

My configurations are :-

我的配置是:-

Chrome - Version 58.0.3029.110 (64-bit)

Geckodriver-v0.16.1-win64

Windows -7

Java - 1.8.1

Chrome - 版本 58.0.3029.110(64 位)

Geckodriver-v0.16.1-win64

Windows 7的

Java - 1.8.1

回答by Sighil

This could be because there are links in you website with hre that looks like #, resources/123.imgwhich are not complete URLS and triggering a get would result in the exception. You should put a check to ensure the urls are valid. This can be donw by using a comparison using link.startsWith("http://") || link.startsWith("https://")

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

There are other places also in which you test would fail.

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

  1. finalListis declared as a List and returned. This ,must be changed to List and should be populated with the link valued. This is because we you have a for loop in which you are calling driver.get(newLink) which would reset all the WebElement objects in the finalListsince they are found earlier and give an exception.

  2. imgtags do not have href. Instead use 'src'.

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

  2. img标签没有href。而是使用“src”。

Here is the code after all those changes. Please be aware that there could be other conditions to check whether the URL is valid or not which I have not listed here.

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

回答by William

I recently got the error org.openqa.selenium.InvalidArgumentException: invalid argument org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:282)

我最近收到错误 org.openqa.selenium.InvalidArgumentException: invalid argument org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:282)

In my case, it was because I was using Cucumber 5.5.0 & when I tried to pass a full url as a variable in the Scenario Outline data table, Cucumber got confused & caused Selenium to have an error.

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