如何在 selenium webdriver for java 中按索引查找元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27208872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:58:48  来源:igfitidea点击:

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

javaseleniumselenium-webdriver

提问by Tim Boland

Im trying to automate the Google Images page:

我正在尝试自动化 Google 图片页面:

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

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.

所有图像都具有相同的类但没有 id,并且结果不断变化。所以我希望能够根据他们的索引点击图像。

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

我知道如何在 C# 中做到这一点......但我无法弄清楚如何在 Java 中的索引中指定。当我尝试选择大于 0 的索引时,出现 IndexOutOfBounds 错误,但我不知道为什么

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

任何帮助将不胜感激

采纳答案by cbarreras

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".

将返回在页面上找到的第一个具有“rg_di”类的元素。

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

该元素中只有一个<a href=... /a>标签。

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

您收到 IndexOutOfBounds 异常,因为您要求第二个(基于零的索引)。如果您将最终的 WebElement 更改为:

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):

这是我的快速版本(注意缺少存储元素,我只需要用 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();
}

回答by bcar

I'd do:

我会做:

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

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

回答by Pardhiva E

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

当我们对相同的 Web 按钮具有相似的对象属性时,此代码运行得非常好,然后使用

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

and then getting

然后得到

 we.get(1).click();

Thank you so much for posting this answer.

非常感谢您发布此答案。

回答by Beytullah uzun

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

另一种解决方案可能是这样的:如果已知 web 元素的类名和索引,则以下代码有效:

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