Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.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
Selenium webdriver 基于selenium的WebDriver_Selenium Webdriver - Fatal编程技术网

Selenium webdriver 基于selenium的WebDriver

Selenium webdriver 基于selenium的WebDriver,selenium-webdriver,Selenium Webdriver,我有一个web元素列表,每个元素包含一个来自谷歌搜索的站点名称 我想使用该列表访问它们!我怎么做到的 WebDriver driver = new FirefoxDriver(); driver.get("http://www.google.com/webhp?complete=1&hl=en"); WebElement element = driver.findElement(By.name("q")); element.sendKeys("test"); element.subm

我有一个web元素列表,每个元素包含一个来自谷歌搜索的站点名称

我想使用该列表访问它们!我怎么做到的

WebDriver driver = new FirefoxDriver();

driver.get("http://www.google.com/webhp?complete=1&hl=en");

WebElement element = driver.findElement(By.name("q"));
element.sendKeys("test");
element.submit();
Thread.sleep(1000);
List<WebElement> sites = driver.findElements(By.xpath(".//*[@id='rso']/div/li/div/h3/a"));

for (WebElement site : sites)
    System.out.println(site.getText());
WebDriver=newfirefoxdriver();
驱动程序。获取(“http://www.google.com/webhp?complete=1&hl=en");
WebElement=driver.findElement(By.name(“q”));
元素。发送键(“测试”);
元素。提交();
睡眠(1000);
List sites=driver.findElements(By.xpath(“./*[@id='rso']/div/li/div/h3/a”);
对于(WebElement站点:站点)
System.out.println(site.getText());

在for循环中如果xpath选择器正确,则每个
站点都是一个
WebElement
表示
元素

因此,要访问链接,只需访问
站点。在for循环中单击()

编辑:新答案

我承认我的回答对你的问题没有帮助。这是另一个我认为适合你需要的解决方案,告诉我它是否适合你

我更改了您的选择器,因为我没有设法使其工作,但可以随意使用您自己的选择器。我使用的那个可以获取谷歌研究的结果链接

pause(xxx)
方法只是调用一个
线程。sleep(xxx)
。我这样做是为了测试方便,但您最好在Selenium API中使用隐式或显式等待实现

下面是代码:

    // Searching "some keywords" on Google
    driver.get("https://www.google.fr");
    driver.findElement(By.id("gbqfq")).sendKeys("some keywords");
    driver.findElement(By.id("gbqfb")).click();
    pause(2000);

    // Storing the results's links
    List<WebElement> sites = driver.findElements(By.cssSelector("#rso div li div h3 a"));

    // Storing the window showing the google's results
    String googleResults = driver.getWindowHandle();

    // Visiting the links       
    for(WebElement site : sites){

        // Perform a SHIFT+click to open in a new window
        Actions builder = new Actions(driver);
        builder.moveToElement(site).keyDown(Keys.SHIFT).click().build().perform();

        // IMPORTANT : Release the SHIFT key,
        //  otherwise the next Google link won't open in a new window
        builder.keyUp(Keys.SHIFT).build().perform();

        pause(500);

        //Switch to the new open window
        for(String winHandle : driver.getWindowHandles()){
            driver.switchTo().window(winHandle);
        }

        pause(2000);


        /*
         * Selenium is now focused on the new window
         * Do you tests here
         */

        // Closing the window
        driver.close();

        // Focusing back on the google's results
        driver.switchTo().window(googleResults);
    }
//在谷歌上搜索“一些关键词”
驱动程序。获取(“https://www.google.fr");
driver.findElement(By.id(“gbqfq”)).sendKeys(“一些关键字”);
driver.findElement(By.id(“gbqfb”))。单击();
暂停(2000年);
//存储结果的链接
列表站点=driver.findElements(由.cssSelector(“#rso div li div h3 a”);
//存储显示谷歌搜索结果的窗口
字符串googleResults=driver.getWindowHandle();
//访问链接
对于(WebElement站点:站点){
//执行SHIFT+单击以在新窗口中打开
动作生成器=新动作(驱动程序);
builder.moveToElement(site).keyDown(Keys.SHIFT).单击().build().perform();
//重要事项:松开SHIFT键,
//否则,下一个谷歌链接将不会在新窗口中打开
builder.keyUp(Keys.SHIFT).build().perform();
暂停(500);
//切换到新打开的窗口
对于(字符串winHandle:driver.getWindowHandles()){
driver.switchTo()窗口(winHandle);
}
暂停(2000年);
/*
*Selenium现在专注于新窗口
*你们这儿有考试吗
*/
//关窗
driver.close();
//关注谷歌的搜索结果
driver.switchTo().window(googleResults);
}

访问”是什么意思?你想点击链接吗?
站点有什么问题。单击()
?你期望的输出是什么,你看到了什么?你有任何例外吗?如果有,是哪一个?你的代码似乎在打印谷歌提供的链接的内部HTML。每个内部HTML都包含从链接重定向的网站的简短描述。您还想做什么?我想验证站点是否可用@用户3794408好的。我会将URL存储在一个列表中。然后我会一个接一个地去有问题的站点,简单地
driver.get()
,并验证是否可以看到任何东西,或者@user3794408感谢您澄清了您实际想要实现的目标。请通过编辑问题将此信息添加到问题正文和标题中。没有必要通读所有的评论来理解一个问题。这是行不通的。当您返回搜索站点时,在第一次单击之后,所有元素都将过时!如果您在新选项卡/窗口中打开每个链接,该怎么办?我想访问每个站点并验证站点是否可用…我该怎么做?@user3794408如果您想编写webcrawler,这里有一个(或两个)可能性:@sing3您能使用Selenium强制在新窗口中打开链接吗?我从来没有这样做过。