-1

I have a list of web-elements, each containing a name of a site from Google search...

I want to access them using that list! How I do that?

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());
barak manos
  • 29,648
  • 10
  • 62
  • 114
  • What does "access" mean? You want to click on the links? What's wrong with `site.click()`? What is your desired output, what do you see instead? Do you get any exceptions and if yes, which? – Petr Janeček Jul 01 '14 at 15:08
  • Your code appears to be printing the inner-HTML of the links provided by Google. Each inner-HTML contains a short description of the website which is redirected from the link. What else do you want to do? – barak manos Jul 01 '14 at 15:13
  • I want to verify if a site is available! – user3794408 Jul 02 '14 at 06:57
  • @user3794408 Alright. I'd store the URLs in a list. Then I'd either go to the sites in question - one by one, simply `driver.get()`, and verify if anything can be seen, or http://stackoverflow.com/a/3584332/1273080. – Petr Janeček Jul 03 '14 at 08:36
  • @user3794408 Thanks for clarifying what you are actually trying to achieve. Please add this information to the question body and title by editing the question. It should not be necessary to read through all comments to understand a question. – oberlies Jul 03 '14 at 12:35

1 Answers1

-2

In your for loop if your xpath selector is correct, each site is a WebElement representing a <a> element

So, to visit a link just do site.click() in your for loop

Edit : New answer

I admit my answer was not helpful for your problem. Here is another solution which I think fits to what you need, tell me if it's working for you.

I have changed you selector because I didn't managed to make it work but feel free to use your own. The one that I use fetches the results links of a Google research.

The pause(xxx) method is simply calling a Thread.sleep(xxx). I did this for testing convenience but you should better use the implicit or explicit wait implementation in Selenium API.

So here is the code :

    // 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);
    }
singe3
  • 2,065
  • 4
  • 30
  • 48
  • 1
    This is not going to work. When you return to the search site, after the first click, all the elements will have gone stale! – SiKing Jul 01 '14 at 18:59
  • What if you open each link in a new tab/window ? – singe3 Jul 02 '14 at 08:21
  • i want to acces each site and verify if a site is available...how i do that? – user3794408 Jul 02 '14 at 08:26
  • @user3794408 If you want to write a webcrawler, here is one (or two) possibility: http://siking.wordpress.com/2014/06/27/simple-web-crawler/ – SiKing Jul 02 '14 at 14:21
  • @singe3 Can you use Selenium to force opening a link a new window? I have never done that. – SiKing Jul 02 '14 at 14:22
  • @SiKing I've edited my answer to provide you with a test case opening each link in a new window – singe3 Jul 04 '14 at 11:43