首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Selenium ChromeDriver:获得锚固元素值的增加时间

Selenium ChromeDriver:获得锚固元素值的增加时间
EN

Stack Overflow用户
提问于 2018-05-09 20:54:37
回答 1查看 39关注 0票数 0

这实际上是一个previous question的延续,但是由于这个问题相当长,并且已经给出了一个赏金的答案,我担心它不会得到任何关注,即使我把它添加到文本的末尾。因此,经理们--我希望你们能决定给这个问题适当发表的最佳决定--还是在前一个问题中。

这一次,我在锚元素上循环的时间越来越长。也就是说,每次调用func()都需要更多的时间。循环只有5次迭代。

代码语言:javascript
复制
void func(WebElement anchorsElement){

    List<WebElement> anchors = anchorsElement.findElements(By.tagName("a"));

    for (WebElement a : anchors) {

        if (a.getAttribute("class").indexOf("a") > 0)
            values.add("A");
        else if (a.getAttribute("class").indexOf("b") > 0)
            values.add("B");
        else if (a.getAttribute("class").indexOf("c") > 0)
            values.add("C");

    }
}

这一次,我对文本不感兴趣,但我需要得到类名。这就是为什么我不能使用给我的前一个问题的解决方案。

这是锚元素的一个示例:

代码语言:javascript
复制
<div class="meetings-8 ">
    <a class="Call-bg Call-s glib-tvene-dW1eVj25 glib-thepart-WdKOwxDM-Wtn9Stg0 glib-partnames-alal;Ashkelon" title="[b]Next meeting:[/b]
alal - Ashkelon
13.05.2018">&nbsp;</a>
    <a class="Call-bg a glib-tvene-j1TJiPdn glib-thepart-Wtn9Stg0-2XrRecc3 glib-partnames-Ashkelon;kdkdkdkd" title="[b]991818&amp;nbsp;[/b](Ashkelon - kdkdkdkd)
09.05.2018">&nbsp;</a>
    <a class="Call-bg b glib-tvene-lhJEIDIa glib-thepart-Wtn9Stg0-xxxpBZl2 glib-partnames-Ashkelon;ieieie" title="[b]920032&amp;nbsp;[/b](Ashkelon - ieieie)
06.05.2018">&nbsp;</a>
    <a class="Call-bg a glib-tvene-IBZf2hYI glib-thepart-Cxq57r8g-Wtn9Stg0 glib-partnames-west-ham;Ashkelon" title="[b]882772&amp;nbsp;[/b](mcmcmcmc - Ashkelon)
29.04.2018">&nbsp;</a>
    <a class="Call-bg a glib-tvene-0U3juSVT glib-thepart-Wtn9Stg0-K6xezbY7 glib-partnames-Ashkelon;kKkssks" title="[b]001991&amp;nbsp;[/b](Ashkelon - kKkssks)
22.04.2018">&nbsp;</a>
    <a class="Call-bg Call-bg-last a glib-tvene-ddkDE7Ld glib-thepart-UDg08Ohm-Wtn9Stg0 glib-partnames-kokok;Ashkelon" title="[b]722726&amp;nbsp;[/b](kokok - Ashkelon)
14.04.2018">&nbsp;</a>
</div>

你能帮忙吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-10 03:16:12

与前面的问题一样,您可以在网页上执行JavaScript以减少通过网络进行的呼叫:

代码语言:javascript
复制
var parent = arguments[0];
// ^ JavaScript is executed as a function call.
//   We need to get the parent element passed as an argument.

Array.from(parent.getElementsByTagName('a'))
//         ^ Get all the "a" tags in the parent
// ^ and convert the HTMLCollection to an Array so we can use reduce
.reduce(
// ^ Reduce will call the following callback on each element with an "accumulated" value (your result) and the current element of the array. The first value of the accumulator is the initial value we provide as the second argument
  (acc, anchor) => {
    if (anchor.classList.contains("a")) {
    //        ^ The classList.contains method will check if the anchor has the class "a" applied.
      acc.push("A");
      // ^ Add "A" to the accumulator
    }
    if (anchor.classList.contains("b")) { acc.push("B"); }
    if (anchor.classList.contains("c")) { acc.push("C"); }
    return acc;
    // ^ Return the accumulated value for the next iteration
  },
  [],
  // ^ Initial value of the accumulator
);

简化后的JavaScript是:

代码语言:javascript
复制
Array.from(arguments[0].getElementsByTagName('a'))
  .reduce((acc, { classList }) => {
    classList.contains("a") && acc.push("A");
    classList.contains("b") && acc.push("B");
    classList.contains("c") && acc.push("C");
    return acc;
  }, []);

你可以像你之前的问题那样在电话里打个比方:

代码语言:javascript
复制
public ArrayList<?> func(WebElement element)
{
    final String JS_TRANSFORM_ANCHOR_CLASSES_TO_VALUES = 
        "return Array.from(arguments[0].getElementsByTagName('a'))\n" +
        "  .reduce((acc, { classList }) => {\n"
        "    classList.contains(\"a\") && acc.push(\"A\");\n"
        "    classList.contains(\"b\") && acc.push(\"B\");\n"
        "    classList.contains(\"c\") && acc.push(\"C\");\n"
        "    return acc;\n"
        "  }, []);\n"

    WebDriver driver = ((RemoteWebElement)table).getWrappedDriver();
    Object result = ((JavascriptExecutor)driver).executeScript(JS_TRANSFORM_ANCHOR_CLASSES_TO_VALUES, element);
    return (ArrayList<?>)result;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50261898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档