xpath - Selenium WebDriver 查找第 n 个元素

标签 xpath selenium webdriver selenium-webdriver

我有点陷入一个问题,无法修复它我必须访问页面上的第 n 个图像,该图像位于 HTML 中的表格中。我需要单击图像以分别为每一行移动到下一个屏幕

我尝试了此处提供的各种解决方案,但由于我使用的是 findElement(by) 语法,因此我无法在这些帮助下运行我的代码

我的代码是:

public void editUser(String userName)
{
    System.out.println("in editUser 1");
    int row = getCellRow(table, userName);
    System.out.println("in editUser 2");

    WebElement  edit = driver.findElement(By.xpath("//*[@class='grid']/tbody/tr[2]/td[8]/a[1]/img"));

    System.out.println("in editUser 3");
    edit.click();
    System.out.println("in editUser 4");
    clickButton(closeButton);
}

错误是:
No such element exception. Unable to locate element: {"method":"xpath", selector"://img[@title='User Management'])[2]

Selenium IDE 可以识别我的元素,但不能识别 Selenium Web 驱动程序!
请指教

最佳答案

例如,使用 [ ] 来引用第 n 个元素

WebElement  edit = driver.findElement(By.xpath("(//*[@class='grid']//img)[n]"));

其中 n 是元素编号

关于xpath - Selenium WebDriver 查找第 n 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12831677/

相关文章:

ruby - capybara ::dsl::模块的未定义方法 'delegate'

python - 无法解析功能 : goog:chromeOptions from invalid argument: unrecognized chrome option: prefs

java - 当元素 DOM 未更改时,Webdriver 陈旧元素异常

python - 如何使用 Selenium 和 Python 在 https ://e. mail.ru/login 中发送密码元素内的文本

xml - XPath 和 Jenkins Plot 插件

xpath - 如何编写Xpath表达式以区分结果?

java - Selenium 2 : Open link in new tab and close tabs

python - 从多个 div (selenium) 中抓取第一个 child 的属性

Selenium IDE 如何设置超时?

c# - 在 Selenium 中获取客户端日志