免责声明:我是StackOverflow的新手,至少在询问我自己的问题方面,所以如果有任何错误或"不良做法",请给我一些指示.
最近在工作中我有理由使用Selenium,并且对这个工具不熟悉,曾经有几次当我为什么某种方式做某事时,我已经开始摸不着头脑了.通常原因后来会变得明显,但在这种情况下不会.
目前我正在编写一些浏览器自动化,其中涉及索引搜索组合,并注意在与我正在为之工作的公司相关的各个站点上显示该组合的结果数量.
对于大多数这些网站,搜索通常会通过我通过Selenium的Select对象处理的关键字和下拉菜单来定义.具体来说,我一直在使用selectByIndex方法来迭代各种组合.
我注意到的一件事是,使用这种方法,我得到的选项列表越远,方法花费的时间越长.当我打开声明时,我发现的代码如下:
/**
* Select the option at the given index. This is done by examing the "index" attribute of an
* element, and not merely by counting.
*
* @param index The option at this index will be selected
*/
public void selectByIndex(int index) {
String match = String.valueOf(index);
boolean matched = false;
for (WebElement option : getOptions()) {
if (match.equals(option.getAttribute("index"))) {
setSelected(option);
if (!isMultiple()) {
return;
}
matched = true;
}
}
if (!matched) { …Run Code Online (Sandbox Code Playgroud)