Selenium:通过XPath提取Alt属性

时间:2015-09-24 08:58:35

标签: java selenium xpath

我在Amazon上自动提取产品变体,我有以下HTML标记:

<ul
    class="a-nostyle a-button-list a-horizontal a-spacing-top-micro swatches swatchesSquare imageSwatches">
    <!-- Please note that in className never append a class with prefix as 'swatch'. It would break something in the twister JS -->
    <li id="color_name_0" class="swatchSelect" data-dp-url="" title="Click to select White">
        <span class="a-list-item">
            <div class="tooltip">
                <span class="a-declarative" data-swatchthumb-action="{"dimIndex":1,"dimValueIndex":0}" data-action="swatchthumb-action">
                    <span id="a-autoid-11" class="a-button a-button-thumbnail a-button-toggle">
                        <span class="a-button-inner">
                            <button id="a-autoid-11-announce" class="a-button-text" type="button">
                                <span class="xoverlay" />
                                <div class="">
                                    <div class="">
                                        <img style="height:36px; width:36px" alt="White"
                                            src="http://ecx.images-amazon.com/images/I/41IrdkWxWOL._SS36_.jpg"/>
                                    </div>
                                    <div class="" style=" " />
                                </div>
                            </button>
                        </span>
                    </span>
                </span>
            </div>
        </span>
    </li>
</ul>

我正在使用以下XPath来提取所有颜色的XPath。

.//*[@id='variation_color_name']/ul/li/span/div/span/span/span/button

现在我想提取每个项目的alt属性,但是当我尝试使用getAttribute("alt")时,它不会返回任何内容。在这种情况下,替代文字将为"White"。我正在查看的产品是:http://www.amazon.com/dp/B00J46VVKE。我正在使用Java。

3 个答案:

答案 0 :(得分:-1)

您可以使用此代码:

public class Stackoverflow extends Init {

    @Test
    public void testToGetAltAttribute() throws InterruptedException {
        System.out.println("Get Attribute....");

        // this element has alt attribute hence that will be displayed.
        assertAndVerifyElement(By.cssSelector("#landingImage"));
        System.out.println("\n#landingImage\n=====================");
        System.out.println(getAttributeOfGivenElement(By.cssSelector("#landingImage"), "alt"));

        // this element do not has alt attribute hence that will not be
        // displayed.
        // it will display msg "element do not have altattribute"
        assertAndVerifyElement(By.id("productTitle"));
        System.out.println("\n#productTitle\n=====================");
        System.out.println(getAttributeOfGivenElement(By.id("productTitle"), "alt"));

    }

    public String getAttributeOfGivenElement(By element, String attributeName) {

        WebElement webElement = getWebDriver().findElement(element);

        if (webElement.getAttribute(attributeName) != null) {
            return webElement.getAttribute("alt");

        } else {
            return "element do not have " + attributeName + "attribute";
        }
    }

    public void assertAndVerifyElement(By element) throws InterruptedException {
        boolean isPresent = false;

        for (int i = 0; i < 5; i++) {
            try {
                if (getWebDriver().findElement(element) != null) {
                    isPresent = true;
                    break;
                }
            } catch (Exception e) {
                // System.out.println(e.getLocalizedMessage());
                Thread.sleep(1000);
            }
        }
        Assert.assertTrue(isPresent, "\"" + element + "\" is not present.");
    }

}

答案 1 :(得分:-1)

如果你有一个id属性,我就不需要去xpath了,除非你有很多具有相同id的按钮。但是,这里是如何获取img元素的属性 -

WebElement btn = driver.findElement(By.id("a-autoid-11-announce"));
String imgColor = btn.findElement(By.tagName("img")).getAttribute("alt");

希望这有帮助。

答案 2 :(得分:-1)

如果您想要所有颜色,则需要获取包含ALT属性的IMG元素。您的XPath以BUTTON结束。请尝试下面的代码。

List<WebElement> colors = driver.findElements(By.cssSelector("ul.imageSwatches img"));
for (WebElement color : colors)
{
    System.out.println(color.getAttribute("alt"));
}

读取CSS选择器,找到具有类UL的{​​{1}}标记,然后找到所有后代imageSwatches标记。您遍历该IMG个标记集合并输出IMG文本。