我有点陷入一个问题,无法修复它我必须访问页面上的第 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/