How to find an Element by index in selenium webdri

2020-04-08 13:54发布

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条回答
Viruses.
2楼-- · 2020-04-08 14:23

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.

查看更多
霸刀☆藐视天下
3楼-- · 2020-04-08 14:28

I'd do:

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

we.get(1) //should get first element in array
查看更多
够拽才男人
4楼-- · 2020-04-08 14:33

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();
}
查看更多
虎瘦雄心在
5楼-- · 2020-04-08 14:36

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();
查看更多
登录 后发表回答