Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find an Element by index in selenium webdriver for java

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

like image 682
Tim Boland Avatar asked Feb 12 '23 11:02

Tim Boland


1 Answers

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();
}
like image 141
cbarreras Avatar answered Feb 14 '23 01:02

cbarreras