如何单击Selenium Webdriver中的所有元素? [英] How Do I Click All Elements in Selenium Webdriver?

查看:169
本文介绍了如何单击Selenium Webdriver中的所有元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已更新

我正在使用:

  • 硒2.53.1
  • Firefox和IE11

我一直试图单击具有相同选择器的所有元素,例如,我想单击标题为"What I Want"的所有元素:

I've been trying to click on all elements with the same selector, for example, I want to click on all the ones with the title "What I Want":

<div id="first_question">
<a class="gibberish1" title="What I Want"></a>
<a class="gibberish2" title="What I Want"></a>
<a class="gibberish3" title="What I Want"></a>
</div>

这是我到目前为止的工作:

Here is what I have working so far:

browser.findElements(by.xpath("//a[@title='What I Want']")).then(function(all_tests){
           for (var i = 0; i < all_tests.length; i++) {
               console.log(all_tests.length);
               all_tests[i].click();
           }
    });

能够识别出我有三个元素,如果直接调用每个元素,那么我可以在按钮上看到它.但是,当我要循环以使其单击每个按钮时,出现错误:

It's able to recognize that I have three elements, and if I call each individual one directly then I'm able to see it click on the button. However, when I want to loop so it clicks on each button, I get an error:

"StaleElementReferenceError:元素不再附加到DOM."

"StaleElementReferenceError: Element is no longer attached to the DOM."

我还增加了5秒的等待时间,但这并没有阻止弹出同样的问题.

I also added a wait of 5 seconds in, but that didn't deter the same issue from popping up.

我做错了什么?我是Selenium的新手,我试图用Javascript来解决这个问题,而不是用Java来找到这种情况的例子.

What am I doing wrong? I'm new to Selenium and I'm trying to figure this out in Javascript, instead of Java which is what I find examples for this situation.

推荐答案

您需要一个一个地找到它们,在页面重新加载时,您的对象将丢失,并且您将获得陈旧的元素异常.

You need to find them one by one, on page reload your objects will be lost and you will get stale element exception.

1)查找所有链接
2)将属性/属性保存在列表/数组中,可以帮助您识别每个链接
3)创建一个循环,在该循环中,针对要搜索的每个属性单击元素

1) find all links
2) save an attribute/attributes in a list/array that can help you identify each link
3) create a loop where for each attribute you are searching for the element and click it

这篇关于如何单击Selenium Webdriver中的所有元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆