简体   繁体   English

如何使用WebDriver查找动态WebElement

[英]How to locate the Dynamic WebElement using WebDriver

Automation Specification- Java based WebDriver 自动化规范-基于Java的WebDriver

Hi 你好
i'm working on a webdriver automation(Java code) to automate bus booking. 我正在研究Webdriver自动化(Java代码)以自动进行巴士预订。
Here is my scenario.... 这是我的情况。

(1)give Departure & Destination - DONE (1)出发与到达- 完成
(2) Pick the Date - DONE (2)选择日期- 完成
(3)Search Bus - DONE (3)搜寻巴士- 完成
(4)View Seats & Select- I Struck here (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-DataTable which contains an image called ' view seat ' in each row. 总线列表显示在dynamic-DataTable中 ,其中每行包含一个名为“ view seat ”的图像。 A click on the viewseat-image will give me the seat-layout where i can select the seats . 单击viewseat-image将为我提供座位布局,我可以在其中选择座位 found the viewseat-images are having different id's & the same class name . 发现viewseat-images具有不同的ID相同的类名 Now i need to locate and click on the viewseat-image whichever i want, using its id. 现在,我需要使用其ID查找并单击所需的viewseat-image。
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不断变化,这使我无法通过静态ID进行定位。
* id is the only unique thing in that coding to differentiate the viewseat-image from another. * id是该编码中唯一可将viewseat-image与另一个区别的唯一事物。 so i can locate it only if i get the id correct. 因此,只有当我获得正确的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. 现在,我的想法是,使用唯一的类名称获取所有vieweat-images的运行时ID,并将其存储在变量中,并使用ID来定位元素。
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。

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

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

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. 您可以使用绝对路径并减少座椅的位置,这可以减少您的工作量;要使用绝对路径,您可以在firefox中安装名为“ firepath”的插件,安装后,您可以选择AbsolutePath或RelativePath 。 With the absolute path you need not use ID to identify the element which is created dynamically. 使用绝对路径,您无需使用ID即可识别动态创建的元素。 The path is identified using the location of the elements within the tags. 使用标签内元素的位置来标识路径。

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]

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

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

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 有关更多说明或示例,我希望: 如何通过Selenium Web Driver自动执行动态页面

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

相关问题 无法使用Selenium Webdriver通过LinkText查找webElement? - Unable to locate webElement by LinkText using selenium webdriver? 我如何在Selenium Webdriver中找到此webelement? - How do i locate this webelement in Selenium Webdriver? 如何使用Java和Selenium WebDriver识别动态WebElement上传图片 - How to identify dynamic WebElement to upload the picture using Java and Selenium WebDriver 如何不使用javascriptExecutor在Selenium Webdriver中突出显示Webelement - How to highlight webelement in selenium webdriver not using javascriptExecutor 如何使用带有Java的Selenium WebDriver将鼠标悬停在Web元素上 - How to mouseover on a webelement using Selenium WebDriver with Java 如何使用Java在Selenium中的以下HTML代码中找到WebElement? - How to locate the WebElement in following HTML code in Selenium using Java? 如何使用WebDriver从下拉菜单中选择WebElement - How to select webelement from dropdown menu using webdriver 如何使用WebDriver验证WebElement是否在视口中显示? - How to verify whether an WebElement is displayed in the viewport using WebDriver? 如何使用Webdriver从复杂的css案例中获取WebElement的颜色? - How To Get Color of WebElement From Complicated css case using Webdriver? 如何使用 Selenium Webdriver 测试 WebElement 属性是否存在 - How to test if a WebElement Attribute exists using Selenium Webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM