Java 如何使用 WebDriver 定位动态 WebElement

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19829921/
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-12 20:33:08  来源:igfitidea点击:

How to locate the Dynamic WebElement using WebDriver

javaautomationwebdriverselenium-webdriverui-automation

提问by Rajesh S

Automation Specification- Java based WebDriver

自动化规范 - 基于 Java 的 WebDriver

Hi
i'm working on a webdriver automation(Java code) to automate bus booking.
Here is my scenario....

嗨,
我正在开发 webdriver 自动化(Java 代码)来自动化巴士预订。
这是我的场景......

(1)give Departure & Destination - DONE
(2) Pick the Date - DONE
(3)Search Bus -DONE
(4)View Seats & Select- I Struck here

(1) 给出出发地和目的地 -完成
(2) 选择日期 -完成
(3) 搜索巴士 -完成
(4) 查看座位和选择 -我点击这里

Now as a search result, i got a list of buses with the option to view the seats. the list of buses are shown in a dynamic-DataTablewhich contains an image called 'view seat' in each row. A click on the viewseat-imagewill give me the seat-layout where i can select the seats. found the viewseat-images are having different id's& the same class name. Now i need to locate and click on the viewseat-image whichever i want, using its id.
But the situation is
* the data table is dynamic, So the id's keep changing which doesn't allow me to locate it by a static id.
* id is the only unique thing in that coding to differentiate the viewseat-image from another. so i can locate it only if i get the id correct.

现在作为搜索结果,我得到了一个带有查看座位选项的巴士列表。公交车列表显示在动态数据表中,其中每行都包含一个名为“查看座位”的图像。单击viewseat-image将为我提供座位布局,我可以在其中选择座位。发现 vieweat-images 具有不同的 id相同的类名。现在我需要使用它的 id 找到并单击我想要的 vieweat-image。
但情况是
* 数据表是动态的,所以 id 不断变化,这不允许我通过静态 id 来定位它。
* id 是该编码中唯一将视图座位图像与其他图像区分开来的唯一内容。所以只有当我得到正确的 id 时我才能找到它。

Now my idea is to, get the runtime id's of all viewseat-images, using the unique class name and store in variables and use the id's to locate the element.
So let me know if there any possibility to get the id's of all images in that data table, using the class name.

现在我的想法是,使用唯一的类名获取所有视图座位图像的运行时 id,并存储在变量中,并使用 id 来定位元素。
所以让我知道是否有可能使用类名获取该数据表中所有图像的 ID。

采纳答案by user2964764

You can use absolute path and take the location of the seat which will reduce your efforts, to take the absolute path you can install a plug-in in firefox called "firepath", after installtion you will have an option for selecting either AbsolutePath or RelativePath. With the absolute path you need not use ID to identify the element which is created dynamically. The path is identified using the location of the elements within the tags.

您可以使用绝对路径并取座位的位置,这将减少您的工作量,要取绝对路径您可以在firefox中安装一个名为“firepath”的插件,安装后您将可以选择AbsolutePath或RelativePath . 使用绝对路径,您无需使用 ID 来标识动态创建的元素。路径是使用标签内元素的位置来标识的。

Example for Absolute Path: //div/div/div/table/tbody/tr/td[2]/div/input[3]

绝对路径示例: //div/div/div/table/tbody/tr/td[2]/div/input[3]

回答by Yuvaraj HK

List<WebElement> elements = driver.findElements(By.className("classname"));    

for(WebElement ele : elements) {
    System.out.println(ele.getAttribute("id"));
} 

回答by Kevin Do

driver.findElement(By.xpath("//a[text()='View seats']")).get(index).click()

You can try this method.But i'm not sure it can work out.I'm looking for a general way which can solve the problem at dynamic pages.

你可以试试这个方法。但我不确定它是否可行。我正在寻找一种可以解决动态页面问题的通用方法。

回答by Himanshu Tewari

Here when the dynamic-Data Table is coming so you can store the size of in the table, Like :

在这里,当动态数据表即将到来时,您可以存储表中的大小,例如:

 int size = driver.findElements(By.xpath("//*[@id='demo_ID']/tbody/tr")).size();

And can set a loop which would read every available row of the table or you can click on them one by one. Like :

并且可以设置一个循环来读取表格的每个可用行,或者您可以逐个单击它们。喜欢 :

String strGetContent = text.getText();
or
text.click(); // As per your need 

Now you can read or click every row of the table, although rows are coming dynamically.I hope it helped you!

现在您可以阅读或单击表格的每一行,尽管行是动态出现的。希望对您有所帮助!

For more explanation or an example I would prefer: How to Automate a Dynamic page through Selenium Web Driver

有关更多解释或我更喜欢的示例:How to Automate a Dynamic page through Selenium Web Driver