使用另一个WebElement时获取WebElement [英] Getting WebElement while using another WebElement

查看:119
本文介绍了使用另一个WebElement时获取WebElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用硒将元素提取到另一个提取的元素内.但是我得到了意想不到的结果.例如,下面的代码在循环中返回相同的数据时间"和tweet时间.请注意,"data-item-id"如何不同,但"data-time"却是相同的.我正在尝试获取与循环中的元素相关的数据时间".知道我的错误是什么吗?

I'm trying to get elements inside another extracted element using selenium. But I'm getting unexpected results. For example the code below returns the same "data-time" and the tweet time in the loop. Notice how the "data-item-id" is different but the "data-time" is the same. I'm trying to get the "data-time" related to the element in the loop. Any idea what my mistake is?

    driver.get("https://twitter.com/CNN");

    List<WebElement> tweets = driver.findElements(By.xpath("//li[contains(@class, 'js-stream-item')]"));

    for (int i = 0; i < tweets.size(); i++) {
        System.out.println(tweets.get(i).getAttribute("data-item-id"));
        WebElement tweetTime = tweets.get(i).findElement(By.xpath("//div[1]/div[2]/div[1]/small/a/span"));
        System.out.println(tweetTime.getAttribute("data-time"));
        System.out.println(tweetTime.getText());
        System.out.println("-----------------------------------");
    }

输出

964597320301121536
1518812897
Feb 16
-----------------------------------
966636880702840834
1518812897
Feb 16
-----------------------------------
966638057217757184
1518812897
Feb 16
-----------------------------------
966636332721360896
1518812897
Feb 16
-----------------------------------
966633486797942784
1518812897
Feb 16
-----------------------------------
966630573740523520
1518812897
Feb 16
-----------------------------------
966627896474460160
1518812897
Feb 16
-----------------------------------
966624849732034560
1518812897
Feb 16
-----------------------------------
966622030526402561
1518812897
Feb 16
-----------------------------------
966611062052343808
1518812897
Feb 16
-----------------------------------
966605886792830976
1518812897
Feb 16
-----------------------------------
966604093719482373
1518812897
Feb 16
-----------------------------------
966602318522265600
1518812897
Feb 16
-----------------------------------
966600547364233216
1518812897
Feb 16
-----------------------------------
966598713903865857
1518812897
Feb 16
-----------------------------------
966594943216242688
1518812897
Feb 16
-----------------------------------
966591153700245505
1518812897
Feb 16
-----------------------------------
966587390805577730
1518812897
Feb 16
-----------------------------------
966587263147741185
1518812897
Feb 16
-----------------------------------
966585227618541568
1518812897
Feb 16
-----------------------------------
966583600673099777
1518812897
Feb 16
-----------------------------------

推荐答案

这是常见问题:要在每个tweets.get(i)中查找元素,您应在XPath的开头指定代表当前节点(上下文)的点:

This is common issue: to find element inside each tweets.get(i) you should specify dot which stands for current node (context) in the beginning of XPath:

".//div[1]/div[2]/div[1]/small/a/span"  # first node found inside current tweet

代替

"//div[1]/div[2]/div[1]/small/a/span"  # first node found in DOM

这篇关于使用另一个WebElement时获取WebElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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