4

Im trying to automate the Google Images page:

https://www.google.com/search?q=pluralsight&biw=1416&bih=685&source=lnms&tbm=isch&sa=X&ei=qGd6VN6bEZTooAT7q4C4BQ&sqi=2&ved=0CAgQ_AUoAw

All the images have the same class but no id and the results are constantly changing. So I would like to be able to click on the images based on their index.

I know how to do it in C#...but I cant figure out how to specify in the index in Java. When I try to select an index beyond 0, I get and IndexOutOfBounds error, but i cant figure out why

WebElement image = chromeDriver.findElement(By.className("rg_di"));
WebElement imageLink = image.findElements(By.tagName("a")).get(1);
imageLink.click();

Here is the entire code im using...any help would be appreciated:

    System.setProperty("webdriver.chrome.driver", "/Users/user/chromedriver");
    WebDriver chromeDriver = new ChromeDriver();
    chromeDriver.get("http://www.google.com");

    WebElement searchBox = chromeDriver.findElement(By.id("gbqfq"));
    searchBox.sendKeys("pluralsight");
    searchBox.sendKeys(Keys.RETURN);

    chromeDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    WebElement imagesLink = chromeDriver.findElement(By.linkText("Images"));
    imagesLink.click();

    WebElement image = chromeDriver.findElement(By.className("rg_di"));
    WebElement imageLink = image.findElements(By.tagName("a")).get(1);
    imageLink.click();

Any help would be greatly appreciated

4 Answers 4

4

In your code:

WebElement image = chromeDriver.findElement(By.className("rg_di"));

will return the first element found on the page with a class of "rg_di".

That element has only one <a href=... /a> tag in it.

You are getting an IndexOutOfBounds exception because you are asking for the second one (zero based indexing). If you change your final WebElement to:

WebElement imageLink = image.findElements(By.tagName("a")).get(0);

The code should work for you with that small change.

This is my quick version (note the lack of storing elements I only need to do one thing with as WebElements):

public static void main(String[] args) {
    // I don't have Chrome installed >.<
    WebDriver driver = new FirefoxDriver();

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    driver.get("http://www.google.com");

    WebElement searchBox = driver.findElement(By.id("gbqfq"));
    searchBox.sendKeys("pluralsight");
    searchBox.sendKeys(Keys.RETURN);

    driver.findElement(By.linkText("Images")).click();

    WebElement image = driver.findElement(By.className("rg_di"));
    image.findElements(By.tagName("a")).get(0).click();

    // super-shortened version:
    // driver.findElement(By.className("rg_di")).findElements(By.tagName("a")).get(0).click();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes i updated the code to use the id of the ordered list that all of the image links are in...so now i can get to each of them by index
3

I'd do:

List<WebElement> we  = chromeDriver.findElements(By.cssSelector(".your-class a"));

we.get(1) //should get first element in array

Comments

2

This code worked very well when we have similar object properties for same web buttons, then using

List<WebElement> we = webdriver.findElements(By.cssSelector(""));

and then getting

 we.get(1).click();

Thank you so much for posting this answer.

Comments

0

Another solution may be like this: If the class name and the index of the web element are known, then following code works:

driver.findElement(By.xpath("(//*[@class='android.widget.ImageView'])[18]")).click();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.