Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用selenium在浏览器中打开多个URL_Java_Selenium - Fatal编程技术网

Java 使用selenium在浏览器中打开多个URL

Java 使用selenium在浏览器中打开多个URL,java,selenium,Java,Selenium,我想用以下命令在浏览器上打开多个url public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "E:\\Java\\Library\\seleniumhq\\chromedriver_win32_2.40.exe"); WebDriver webDriver = new ChromeDriver(); openNewTab(webDriver, "ht

我想用以下命令在浏览器上打开多个url

public static void main(String[] args) {
    System.setProperty("webdriver.chrome.driver", "E:\\Java\\Library\\seleniumhq\\chromedriver_win32_2.40.exe");
    WebDriver webDriver = new ChromeDriver();

    openNewTab(webDriver, "http://dantri.com.vn/phap-luat.htm", 1);
    openNewTab(webDriver, "http://dantri.com.vn/xa-hoi.htm", 2);
    openNewTab(webDriver, "http://dantri.com.vn/the-gioi.htm", 3);
    openNewTab(webDriver, "http://dantri.com.vn/the-thao.htm", 4);
    openNewTab(webDriver, "http://dantri.com.vn/giao-duc-khuyen-hoc.htm", 5);

}

public void openNewTab(WebDriver webDriver, String url, int position) {
    ((JavascriptExecutor) webDriver).executeScript("window.open()");

    ArrayList<String>  tabs = new ArrayList<>(webDriver.getWindowHandles());
    System.out.println("tabs : " + tabs.size() + " >position: " + position + " >\t" + url);
    webDriver.switchTo().window(tabs.get(position));

    webDriver.get(url);
}
publicstaticvoidmain(字符串[]args){
System.setProperty(“webdriver.chrome.driver”,“E:\\Java\\Library\\seleniumhq\\chromedriver\u win32\u 2.40.exe”);
WebDriver WebDriver=新的ChromeDriver();
openNewTab(webDriver,“http://dantri.com.vn/phap-luat.htm", 1);
openNewTab(webDriver,“http://dantri.com.vn/xa-hoi.htm", 2);
openNewTab(webDriver,“http://dantri.com.vn/the-gioi.htm", 3);
openNewTab(webDriver,“http://dantri.com.vn/the-thao.htm", 4);
openNewTab(webDriver,“http://dantri.com.vn/giao-duc-khuyen-hoc.htm", 5);
}
public void openNewTab(WebDriver、WebDriver、字符串url、int位置){
((JavascriptExecutor)webDriver.executeScript(“window.open()”);
ArrayList tabs=新建ArrayList(webDriver.getWindowHandles());
System.out.println(“制表符:“+tabs.size()+”>position:“+position+”>\t”+url);
webDriver.switchTo().window(tabs.get(position));
webDriver.get(url);
}
虽然浏览器选项卡已打开,但tabs.size()保持不变,从而导致错误

tabs : 2 >position: 1 > http://dantri.com.vn/phap-luat.htm
tabs : 2 >position: 2 > http://dantri.com.vn/xa-hoi.htm
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
    at java.util.ArrayList.rangeCheck(ArrayList.java:657)
    at java.util.ArrayList.get(ArrayList.java:433)
    at test.test.NewClass.openNewTab(NewClass.java:37)
    at test.test.NewClass.<init>(NewClass.java:25)
    at test.test.NewClass.main(NewClass.java:43)
选项卡:2>位置:1>http://dantri.com.vn/phap-luat.htm
选项卡:2>位置:2>http://dantri.com.vn/xa-hoi.htm
线程“main”java.lang.IndexOutOfBoundsException中的异常:索引:2,大小:2
位于java.util.ArrayList.rangeCheck(ArrayList.java:657)
获取(ArrayList.java:433)
在test.test.NewClass.openNewTab(NewClass.java:37)
在test.test.NewClass.(NewClass.java:25)
位于test.test.NewClass.main(NewClass.java:43)

请帮我修复它

将openNewTab方法更改为静态方法。我没有得到异常和预期的响应

修改代码:

public class MultiTab {

    public static void main(String args[]){
        System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
        WebDriver webDriver =new ChromeDriver();
        openNewTab(webDriver, "http://dantri.com.vn/phap-luat.htm", 1);
        openNewTab(webDriver, "http://dantri.com.vn/xa-hoi.htm", 2);
        openNewTab(webDriver, "http://dantri.com.vn/the-gioi.htm", 3);
        openNewTab(webDriver, "http://dantri.com.vn/the-thao.htm", 4);
        openNewTab(webDriver, "http://dantri.com.vn/giao-duc-khuyen-hoc.htm", 5);
    }

    public static void openNewTab(WebDriver webDriver, String url, int position) {
        ((JavascriptExecutor) webDriver).executeScript("window.open()");
        ArrayList<String> tabs = new ArrayList<>(webDriver.getWindowHandles());
        System.out.println("tabs : " + tabs.size() + " >position: " + position + " >\t" + url);
        webDriver.switchTo().window(tabs.get(position));
        webDriver.get(url);
    }
}
tabs : 2 >position: 1 > http://dantri.com.vn/phap-luat.htm
tabs : 3 >position: 2 > http://dantri.com.vn/xa-hoi.htm
tabs : 4 >position: 3 > http://dantri.com.vn/the-gioi.htm
tabs : 5 >position: 4 > http://dantri.com.vn/the-thao.htm
tabs : 6 >position: 5 > http://dantri.com.vn/giao-duc-khuyen-hoc.htm

将openNewTab方法更改为静态方法。我没有得到异常和预期的响应

修改代码:

public class MultiTab {

    public static void main(String args[]){
        System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
        WebDriver webDriver =new ChromeDriver();
        openNewTab(webDriver, "http://dantri.com.vn/phap-luat.htm", 1);
        openNewTab(webDriver, "http://dantri.com.vn/xa-hoi.htm", 2);
        openNewTab(webDriver, "http://dantri.com.vn/the-gioi.htm", 3);
        openNewTab(webDriver, "http://dantri.com.vn/the-thao.htm", 4);
        openNewTab(webDriver, "http://dantri.com.vn/giao-duc-khuyen-hoc.htm", 5);
    }

    public static void openNewTab(WebDriver webDriver, String url, int position) {
        ((JavascriptExecutor) webDriver).executeScript("window.open()");
        ArrayList<String> tabs = new ArrayList<>(webDriver.getWindowHandles());
        System.out.println("tabs : " + tabs.size() + " >position: " + position + " >\t" + url);
        webDriver.switchTo().window(tabs.get(position));
        webDriver.get(url);
    }
}
tabs : 2 >position: 1 > http://dantri.com.vn/phap-luat.htm
tabs : 3 >position: 2 > http://dantri.com.vn/xa-hoi.htm
tabs : 4 >position: 3 > http://dantri.com.vn/the-gioi.htm
tabs : 5 >position: 4 > http://dantri.com.vn/the-thao.htm
tabs : 6 >position: 5 > http://dantri.com.vn/giao-duc-khuyen-hoc.htm


请将“字符串”参数添加到ArrayList。此处tabs=newarraylist(webDriver.getWindowHandles());您已经测试过了,但仍然有问题请通过删除此行中的Static关键字来检查“Static ArrayList tabs”仍然不起作用您是否收到相同的异常?请使用调试器进行检查,在调试器中获取空值/responce。请将“String”参数添加到ArrayList。此处tabs=newarraylist(webDriver.getWindowHandles());您已经测试过了,但仍然有问题请通过删除此行中的Static关键字来检查“Static ArrayList tabs”仍然不起作用您是否收到相同的异常?请使用调试器进行检查,在调试器中您得到的是空值/响应。我仍然有故障,请您给我您使用的selemium版本和chromedriver。我使用的是Selenium版本:3.11.0I检查了命令,它工作不正常,当tabs=new ArrayList(webDriver.getWindowHandles())时可能出错;没有更新值。我希望语句比继续帮助meif传递两个URL及其开始的3个选项卡更稳定。。。我只想为2个urlsI打开2个选项卡仍然存在故障,您能给我您使用的selemium版本和chromedriver吗?我正在使用Selenium版本:3.11.0我检查了命令它不能正常工作,当tabs=new ArrayList(webDriver.getWindowHandles())时可能错误;没有更新值。我希望语句比继续帮助meif传递两个URL及其开始的3个选项卡更稳定。。。我只想为2个URL打开2个选项卡