如何使用selenium webdriver从html中提取H1,H2,H3标记值

时间:2016-04-04 11:48:46

标签: selenium selenium-webdriver

1.HTML

<div id="page-filter1">
**<h3>**
    Browse Category
**</h3>**
<ul class="mt-accordion multiple">
   <li id="phdesktopbody_0_phdesktopfilterbycategory_0_liAllProduct" class="cls-8dcbcbac-2fef-4231-9641-d61818abe0e0 item-1 odd first odd">
        <a id="phdesktopbody_0_phdesktopfilterbycategory_0_hypAllProducts" href="/en-us/products">All Products</a>
    </li>

2

<div class="span12">
    <h3 class="onelayout-heading">
        <strong><em>Callout <sub>&copy;</sub>itle<sup>x</sup></em></strong>
    </h3>
</div>
<div id="phdesktopbody_0_phdesktopflexiblepromo_0_phdesktoppromocontentarea6b299d9421684ceaaed7c23ebee57f57_0_panelSubheadlineandCTASection" class="span7 pull-left">

每页中有超过1个H2和&amp; H3标签, 类名和描述每次都会更改,请帮助我识别标签并从中提取值。

1 个答案:

答案 0 :(得分:0)

嗨Arjun Vc如果类名和描述每次都改变相同的beloe代码就行了,请在下面做。

driver.get("http://www.seleniumhq.com"); // link to your  web page

        // working with H1 H2 .... tags 

        String TagToWorkWith = "h1"; // here simply change the tag name on which you want to work
        List<WebElement> myTags =  driver.findElements(By.tagName(TagToWorkWith));

        // now extracting the vale 
        // this for loop will print/extract all the values for tag 'H1'.
        for(int i=0;i<myTags.size();i++){
            // extracting tags text
            System.out.println(TagToWorkWith + " value is : " + myTags.get(i).getText());
        }