验证< div>之间的文字使用Selenium [英] Verifying text between <div> using Selenium

查看:194
本文介绍了验证< div>之间的文字使用Selenium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要验证网页上的特定广告位是否已加载。我在 main()函数中使用Selenium WebDriver(我应该使用JUnit测试用例吗?)。

I need to verify whether specific slot on webpage is loaded or not. I am using Selenium WebDriver in main() function for that (should I use a JUnit test case?).

在具有特定ID的任何< div> 之间检索文本的方法有哪些?

What are ways to retrieve text between any <div> having a specific id?

如果给定此页

<div id="center-2" class="promo">
    <div class="unified_widget rcmBody">
        <span class="logo"><img src="someimage.gif" /></span>
        <span class="headline">Make money by Selling items on Amazon</span>
        <span class="link"><a href="/content/sell-on-amazon.htm">Selling On Amazon</a></span>
        <span class="body">Sell your items on Amazon.com. Amazon can help you grow your business and reach more customers.</span>
        <div class="h_rule"></div>
    </div>
</div>

< div id = center-2> 给出,我需要提取

通过在亚马逊上销售商品赚钱,

在亚马逊上销售,

在Amazon.com上销售您的商品。亚马逊可以帮助您拓展业务并吸引更多客户。

"Make money by Selling items on Amazon",
"Selling On Amazon",
"Sell your items on Amazon.com. Amazon can help you grow your business and reach more customers."

我应该使用哪种方法?

推荐答案

为确保元素已加载,请使用隐式和显式等待

To be sure an element is loaded, use Implicit and Explicit waits.

通常,当您需要与元素进行交互时,隐含的等待就足够了。一旦设置,它就用于每个元素查找。根据我的经验,95%的情况就足够了。

Usually, when you need to interact with the element, an implicit wait is enough. Once it's set, it's used for every element lookup. In my experience, it's enough for 95 % of cases.

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

如果你需要一些更高级的等待(或只是使用等待一次),显式等待就是你。

If you need some more advanced waiting (or just use the wait once), explicit wait is there for you.

有三种常用方法可以检索具有特定 id的元素我知道:

There are three usually used ways to retrieve an element with a specific id I know about:


  1. 首选方式:

  1. The preferred way:

driver.findElement(By.id("elemId"));


  • 如果你需要从一个元素开始然后搜索它的后代,这是一个好方法: / p>

  • A good way if you need to begin with an element and then search its descendants:

    driver.findElement(By.cssSelector("#elemId"));
    driver.findElement(By.xpath("id('elemId')"));
    


  • 许多人使用的方式,因为他们对XPath感到满意:

  • The way a lot of people uses, because they're comfortable with XPath:

    driver.findElement(By.xpath("//*[@id='elemId']"));
    


  • 了解更多关于 CSS选择器 XPath ,请参阅网络上的任何教程。

    To know more about CSS selectors and XPath, see any tutorial on the web.

    关于文字:

    String text = driver.findElement(By.id("center-2")).getText();
    System.out.println(text);
    

    输出


    通过在亚马逊上销售物品赚钱在亚马逊上销售您在Amazon.com上销售
    物品。亚马逊可以帮助您拓展业务,并为更多客户提供

    Make money by Selling items on Amazon Selling On Amazon Sell your items on Amazon.com. Amazon can help you grow your business and reach more customers.

    如果您想独立完成这三件事,请使用例如:

    If you want to get the three things independently, use e.g.:

    String headline = driver.findElement(By.cssSelector("#center-2 .headline")).getText();
    System.out.println(headline);
    
    String link = driver.findElement(By.cssSelector("#center-2 .link")).getText();
    System.out.println(link);
    
    String body = driver.findElement(By.cssSelector("#center-2 .body")).getText();
    System.out.println(body);
    

    这篇关于验证&lt; div&gt;之间的文字使用Selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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