简体   繁体   English

在使用Selenium webdriver时,为什么我们使用链表来收集链接或下拉内容与mutliple匹配?

[英]While working with Selenium webdriver, why do we use linked list for gathering links or dropdown contents with mutliple matches?

Example code is something like this, (this was an interview question asked for me recently) 示例代码是这样的(这是最近向我询问的面试问题)

List linkElements = driver.findElements(By.tagName("a")); 列表linkElements = driver.findElements(By.tagName(“a”));

I would say, through List you can dynamically add, access and remove objects of the same type. 我会说,通过List您可以动态添加,访问和删除相同类型的对象。 Also, it won't mind even if it has No contents. 而且,即使它没有内容也不介意。

Generally you choose a Data Structure according to your needs. 通常,您可以根据需要选择数据结构。 Here while doing a findElements() search we are saying that we want all the elements with the given structure in DOM and we can't be always sure about it's size in advance. 在执行findElements()搜索时,我们说我们希望DOM中具有给定结构的所有元素,并且我们不能总是事先确定它的大小。 Using a fixed sized array in such conditions won't make much sense. 在这样的条件下使用固定大小的阵列没有多大意义。

I hope it helps :) 我希望它有帮助:)

List represents an ordered list of objects, meaning you can access the elements of a List in a specific order, and by an index too. List表示对象的有序列表,这意味着您可以按特定顺序访问List的元素,也可以通过索引访问。 You can also add the same element more than once to a List. 您还可以多次向List添加相同的元素。 List allows null elements and you can have many null objects in a List List允许使用null元素,并且List中可以包含许多null对象

You will get all the result in a particular order one by one. 您将逐个获得特定顺序的所有结果。 It is also allow you to add duplicates. 它还允许您添加重复项。 Our result can have duplicates which mainly we need in automation , but if your requirement is different and you do not need duplicates then you can use other collection type.If you use set then it will not allow duplicates and it's an unorder presentation of object. 我们的结果可能有重复,主要是我们在自动化中需要的,但如果您的要求不同而且您不需要重复,那么您可以使用其他集合类型。如果您使用set,那么它将不允许重复,并且它是对象的无序表示。

We use List because when we are using findElements() instead of findElement() then we are expecting that locator will return us more than 1 element(Not in every case or scenario). 我们使用List,因为当我们使用findElements()而不是findElement()我们期望locator将返回超过1个元素(不是在每种情况或场景中)。 So it's a good practice to use List so our data is saved in list in a ordered manner so we can use them properly. 因此,使用List是一个很好的做法,因此我们的数据以有序的方式保存在列表中,以便我们可以正确使用它们。

Generally I am using List in below manner:- 一般来说,我以下面的方式使用List: -

 List<WebElement> allOptions = dropDown.findElements(By."OUR Locator");
    for ( WebElement we: allOptions) { 
        dropDown.sendKeys( Keys.DOWN ); //simulate visual movement
        sleep(250);       
        if ( we.getText().contains( text ) ) select.selectByVisibleText("Value1");
    }

You can also do it in many ways refer below:- 您也可以通过多种方式参考以下内容: -

https://sqa.stackexchange.com/questions/8029/how-to-iterate-a-list-of-webelements-and-print-the-values-one-by-one-to-perform https://sqa.stackexchange.com/questions/8029/how-to-iterate-a-list-of-webelements-and-print-the-values-one-by-one-to-perform

Here is more detail version which help you to identify that when to use list:- 这里有更详细的版本,可以帮助您确定何时使用列表: -

http://java67.blogspot.in/2013/01/difference-between-set-list-and-map-in-java.html http://java67.blogspot.in/2013/01/difference-between-set-list-and-map-in-java.html

Adding and Accessing Elements 添加和访问元素

To add elements to a List you call its add() method. 要向List添加元素,请调用其add()方法。 This method is inherited from the Collection interface. 此方法继承自Collection接口。 Here are a few examples: 这里有一些例子:

List listA = new ArrayList();

listA.add("element 1");
listA.add("element 2");
listA.add("element 3");

You can access them via index as below:- 您可以通过索引访问它们,如下所示: -

String element0 = listA.get(0);
String element1 = listA.get(1);
String element3 = listA.get(2);

System.out.println(element0+" "+element1+" "+element3 ); System.out.println(element0+" "+element1+" "+element3 );

Hope it will help you :) 希望它能帮到你:)

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

相关问题 下拉功能在Selenium Webdriver中不起作用 - dropdown functionality not working in selenium webdriver 为什么在链表的节点中插入值时需要一个临时节点? - Why do we need a temporary node while inserting values in a node in Linked list? 使用 selenium webdriver 找不到下拉列表元素 - Dropdown list element not found using selenium webdriver Selenium Webdriver无法在下拉列表中选择值 - Selenium Webdriver cannot choose a value in a dropdown list 为什么我们在 selenium webdriver 中需要番石榴? - Why we need guava in selenium webdriver? 我们可以在 Selenium WebDriver 中使用抽象类吗? - Can we use Abstract Class in Selenium WebDriver? 实现链接列表时,为什么要使用2个构造函数? - Why should we use 2 constructors when implementing Linked List? 为什么说链接列表插入是固定时间? - Why do we say linked list inserts are constant time? 在使用Selenium WebDriver和Java时,我无法使用WebDriver的click()函数(无例外) - While working with Selenium WebDriver and Java I am not able to use click() function of WebDriver (giving no exception) 如何在从第二个窗口切换到第三个窗口时通过 selenium webdriver 处理多个窗口 - How to handle mutliple windows through selenium webdriver while switching from second to third window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM