简体   繁体   English

加速硒webdriver?

[英]Speed up selenium webdriver?

I'm using selenium webdriver to automate web page usage. 我正在使用Selenium Webdriver自动执行网页使用。 Headless browser not allowed. 不允许使用无头浏览器。

Selenium seems rather slow at finding multiple elements on a single page that is fully loaded. Selenium在完全加载的单个页面上查找多个元素似乎相当慢。

Does anyone have any tips for how to speed things up? 有没有人有任何技巧来加快速度? I am generally searching for objects via xpath. 我通常通过xpath搜索对象。

I have searched google and read similar SO posts . 我已经搜索过Google并阅读过类似的帖子 I am looking for new ideas 我正在寻找新想法

in this case i like to create a org.​w3c.​dom Document using the page source and then parse it using the javax.xml libary: 在这种情况下,我想使用页面源创建一个org.w3c.dom文档,然后使用javax.xml库解析它:

public static Document getWebpageDocument_fromSource(String source) throws InterruptedException, IOException {
    try {
        HtmlCleaner cleaner = new HtmlCleaner();
        CleanerProperties props = cleaner.getProperties();
        props.setAllowHtmlInsideAttributes(true);
        props.setAllowMultiWordAttributes(true);
        props.setRecognizeUnicodeChars(true);
        props.setOmitComments(true);

        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        try {
            builder = builderFactory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }

        TagNode tagNode = new HtmlCleaner().clean(source);

        Document doc = new DomSerializer(new CleanerProperties()).createDOM(tagNode);

        return doc;
    } catch (ParserConfigurationException ex) {
        ex.printStackTrace();
        return null;
    }
}

and then accessing elements via xpath like this: 然后像这样通过xpath访问元素:

String myXpathStr = "//*[@id='news-main']/div";
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList articleBlocks = (NodeList)xPath.compile(myXpathStr).evaluate(doc, XPathConstants.NODESET);

hope that helps. 希望能有所帮助。 i also agree with the other answers that id and css are faster. 我也同意id和css更快的其他答案。 i've found xpath to be more powerful but I don't have a lot of experience with css paths 我发现xpath更加强大,但是我对CSS路径没有太多的经验

I search by id , class name and other easily identifiable elements. 我通过idclass name和其他易于识别的元素进行搜索。 But speed is going to be based on things like network connection and hardware. 但是速度将取决于网络连接和硬件。 You can alway use the HTMLDriver , as this will be the quickest version of the driver. 您可以HTMLDriver使用HTMLDriver ,因为它将是驱动程序的最快版本。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM