简体   繁体   English

Selenium Webdriver-从动态表访问数据

[英]Selenium Webdriver - accessing data from dynamic table

I am automating some of the functional test cases in a web application using Selenium webdriver and Java. 我正在使用Selenium webdriver和Java在Web应用程序中自动化一些功能测试用例。 I have a form where I can add new employee. 我有一个可以添加新员工的表格。 When a new employee is added, the added employee details can be seen in another page. 添加新员工后,可以在另一个页面中看到添加的员工详细信息。 I want to verify the added employee details and finally delete the employee from the view page. 我想验证添加的员工详细信息,最后从视图页面删除该员工。 Each of the added employee details are showing in separate tables in the view page. 每个添加的员工详细信息都显示在视图页面的单独表格中。 The xpath of each table is as follows 每个表的xpath如下

.//*[@id='contentSec']/div/div[1]  
.//*[@id='contentSec']/div/div[2]
.//*[@id='contentSec']/div/div[3]
and so on...

The issue is that how can I get the xpath of the last added employee table in order to verify employee details. 问题在于,如何获取上一个添加的employee表的xpath,以验证员工详细信息。 I can't search using employee name or any employee details since multiple employees may have same details. 我无法使用员工姓名或任何员工详细信息进行搜索,因为多个员工可能具有相同的详细信息。 I want a generalized method to access the employee details that should work always. 我想要一种通用的方法来访问应始终工作的员工详细信息。 I used 我用了

List<WebElements> elements=driver.findElements(By.xpath(".//*[@id='contentSec']/div"));

but

elements.size();

always returns 1 even if multiple tables exists. 即使存在多个表,也总是返回1。 Is there any method to access last added employee details? 有什么方法可以访问最近添加的员工详细信息?

It sounds like you are missing the /div at the end to get inner div s inside a parent div : 这听起来像你缺少/div在年底获得内div父母在s div

.//*[@id='contentSec']/div/div

Is there any method to access last added employee details? 有什么方法可以访问最近添加的员工详细信息?

In this case, you can use last() : 在这种情况下,您可以使用last()

.//*[@id='contentSec']/div/div[last()]

So ,You are doing a small mistake in your approach of finding elements. 因此,您在查找元素的方法上犯了一个小错误。

it should be findElements and not findElement 它应该是findElements而不是findElement

List<WebElements> elements=driver.findElements(By.xpath(".//*[@id='contentSec']/div"));

driver.findElement will always look for the first accurance of the element thats why you are always getting 1 when trying to get the size using elements.size() driver.findElement始终会寻找元素的第一个精度,这就是为什么在尝试使用elements.size()来获取size时总是得到1原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM