Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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
扫描网站中的所有链接(URL),并在selenium get()方法中使用每个链接_Url_Selenium_Selenium Webdriver - Fatal编程技术网

扫描网站中的所有链接(URL),并在selenium get()方法中使用每个链接

扫描网站中的所有链接(URL),并在selenium get()方法中使用每个链接,url,selenium,selenium-webdriver,Url,Selenium,Selenium Webdriver,我需要获取网站中的所有URL,以便使用Selenium get()方法打开网页。在打开一个页面后,我打算从该页面获取一些数据,然后转到下一个链接 你能帮我找到做这件事的最佳方法,并提供一个相同的示例代码吗。可能还有其他方法,但这是我想到的第一种方法 // Create your driver of choice WebDriver fDriver; fDriver = new FirefoxDriver(); // Direct the driver to th

我需要获取网站中的所有URL,以便使用Selenium get()方法打开网页。在打开一个页面后,我打算从该页面获取一些数据,然后转到下一个链接


你能帮我找到做这件事的最佳方法,并提供一个相同的示例代码吗。

可能还有其他方法,但这是我想到的第一种方法

    // Create your driver of choice
    WebDriver fDriver;
    fDriver = new FirefoxDriver();

    // Direct the driver to the site that you want to get all the links from
    fDriver.get("Site URL here...");

    // Grab all the anchor tags on the page you're currently on.
    List<WebElement> anchors = fDriver.findElements(By.tagName("a"));

    // Create a 2nd List to hold the URLs of the anchor tags.
    List<String> allURLs = new ArrayList<String>();

    // Iterate through all the anchors that you got.
    for (WebElement a : anchors) {

        // Print out the URL of the anchor.
        System.out.println(a.getAttribute("href"));

        // Store the URL of the List.
        allURLs.add(a.getAttribute("href"));
    }

    // Now just get the URL you want to use from the list...
    String siteURL = allURLs.get(0);
    // and enter it into the get() method of the driver.
    fDriver.get(siteURL);
//创建您选择的驱动程序
网络驱动程序;
fDriver=新的FirefoxDriver();
//将驱动程序指向您希望从中获取所有链接的站点
fDriver.get(“此处的站点URL…”);
//抓取您当前所在页面上的所有锚定标签。
列表锚=fDriver.findElements(按.tagName(“a”));
//创建第二个列表以保存锚定标记的URL。
List allURLs=new ArrayList();
//迭代所有你得到的锚。
for(WebElement a:锚定){
//打印出锚的URL。
System.out.println(a.getAttribute(“href”);
//存储列表的URL。
添加(a.getAttribute(“href”);
}
//现在只需从列表中获取要使用的URL。。。
字符串siteURL=allURLs.get(0);
//并将其输入到驱动程序的get()方法中。
fDriver.get(siteURL);

通过get方法使用所有URL的第二部分取决于您自己,但我认为我已经给了您一个良好的开端。希望有帮助

请你自己试一试,并向我们展示你的努力,如果它仍然不起作用,我们将很乐意提供帮助。。