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

标签 java arrays selenium selenium-webdriver webdriver

我在 Arraylist 中添加了来自网页的所有链接,然后一一点击所有 URL。

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

最佳答案

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

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

  1. finalList 声明为 List 并返回。这必须更改为 List 并且应该用有值(value)的链接填充。这是因为我们有一个 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();
                }
            }
        }
    }

关于java - org.openqa.selenium.InvalidArgumentException : unknown error: unsupported protocol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44202217/

相关文章:

java - java中的拼字游戏数组

python - 无法使用 Python 在 Ubuntu 上运行 Selenium

java - 如何在 Spring Boot 中加载和遍历属性文件

javascript - 标题大小写 JavaScript

java - 如何找到玩家上传数据中最大的值(value)

java - 根据元素字段对 ArrayList 进行排序

javascript - selenium webdriver 中的窗口对象是空数组

java - 使用 Selenium 访问 <ul> 中的 HTML 元素

java - 将 BufferedImage 添加到 PDFBox 2.0 文档

java - equalsIgnoreCase 与 Java 中的 regionMatches。哪个效率高?