Selenium相对定位器从WebElement进行搜索的速度非常慢 [英] Selenium relative locator searches from a WebElement extremely slow

查看:891
本文介绍了Selenium相对定位器从WebElement进行搜索的速度非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium 2.0,Firefox 11.0和Java处理表.我有一个由td单元格组成的表格元素,其中一些包含span元素中包含的文本,其他一些包含其value属性中包含文本的输入元素.我的目标是获取每个单元格的文本,以便我可以输出表内容并将它们与期望值进行比较.我以为我会做这样的事情:

I am using Selenium 2.0, Firefox 11.0, and Java to process a table. I have a table element composed of td cells, some which contain text included in a span element, others which contain input elements which have text in their value attributes. My goal is to get the text of every cell so I can output the table contents and compare them against expected values. I thought I would just do something like this:

Locate the table WebElement by id
List<WebElement> cells = tableElem.findElements(By.xpath(".//td"));

然后,我将遍历所有单元格并使用xpath".//input"运行findElements,如果列表为空,则将在webElement上运行getText,如果列表不为空,则将在输入元素.

Then I would loop through all the cells and run findElements with the xpath ".//input" and if the list was empty I would run getText on the webElement, and if the list wasn't empty I would run getAttribute on the input element.

但是令我惊讶的是,这花了几分钟才在firefox上运行(我恐怕要在IE上进行尝试,而IE应该在此进行测试).当我调试时,很明显瓶颈是来自td的.//input搜索,这使我丧命.它需要十秒钟以上的时间,因此即使只有几个单元格,我的测试也将永远耗费时间.我已经尝试过对xpath进行各种细微的改动,尝试使用CSS选择器,并继续获得相同的结果.

But to my surprise, this took several minutes to run on firefox (I'm afraid to try it on IE, which is where its supposed to be tested). When I debug it is obvious that the bottleneck is the .//input search from the td which is killing me. It is upwards of ten seconds, and so even with just a few cells my tests are taking forever. I've tried all sorts of minor variations to the xpath, tried going to css selectors, and continue to get the same results.

我想要一些有关如何以不同方式解决此问题或如何优化当前方法的建议.我希望这只需要几秒钟.

I want some advice about how to either tackle this problem differently or how to optimize my current method. I was hoping this would only take a couple of seconds.

我提供了一些示例代码,这些代码应说明我所遇到的速度下降.这不是我正在抓屏的网站,但是速度却是相同的:

I've included some sample code that should illustrate the slowdown I'm experiencing. This is not the website I'm screen scraping, but the slowness is the same:

    webDriver.navigate().to("https://accounts.google.com/NewAccount");
    List<WebElement> TDxpath = webDriver.findElements(By.xpath("//td"));
    List<WebElement> TDcss = webDriver.findElements(By.cssSelector("td"));
    for (WebElement td : TDcss) {
        List<WebElement> q = td.findElements(By.cssSelector("input"));
    }
    for (WebElement td : TDxpath) {
        List<WebElement> r = td.findElements(By.xpath(".//input"));
    }

推荐答案

您真的需要浏览器吗?您可以尝试HtmlUnitDriver,这将非常快!

Do you really need a browser? You could try HtmlUnitDriver, that will be blazingly fast!

或者您也可以将其作为JS来完成,它只需要花费一小部分时间,并且

Or you could do it as a JS, that also only takes a fraction of time and you can get Lists from the script:

(JavascriptExecutor)driver.executeScript(
    "var tds = document.getElementsByTagName('td');"
    "for (var i = 0; i < tds.length; i++) {" +
    "   var inputs = tds[i].getElementsByTagName('input');" +
    "}"
    );

这篇关于Selenium相对定位器从WebElement进行搜索的速度非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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