如何在selenium中的标记之间提取数据

问题描述 投票:1回答:3

如何在“帮助文件”类之后提取数据。在这里,这个文本HELP FILE是一个链接,当点击它导致我另一种形式。你们中的任何人都可以建议我如何处理它。我是Selenium的新手,我在这里受到了打击。我尝试通过提取xpath但它给了我主页的路径 我正在使用selenium webdriver和eclipse ide。我的项目只支持IE。

<TD align="left" width="185px" NOWRAP valign="top">
    <a class="wlcmhding"><IMG  SRC="../../images/image1.jpg" border="0"></a><BR>
          <a href="/prj1//ex12Servlet?action=exampleForm" class="wlcmlink">HELP FILE</a><BR>
    <a href="/prj1//ex12Servlet?action=exampleForm" class="wlcmlink">CODE FILE</a><BR>
</TD>   
selenium-webdriver
3个回答
1
投票

试试这段代码:

// Assume driver in initialized  properly
String strText = driver.findElement(By.id("Locator id")).getText();
// Print the text of the Web Element
System.out.println(strText); 

0
投票

这是一个答案,它将返回具有相同String查询(帮助文件)的标记内的WebElement。希望这会对你有所帮助,但不确定我是否理解了这个问题。

当你想根据其中的文本操作WebElement时,它会让我感到震惊,因此这种方法很可能适合你。

public WebElement getMessage(final WebDriver driver, final String query) {
    List<WebElement> wlcmLinks = driver.findElements(By.className("wlcmlink"));
    WebElement finalLink = null;
    Iterator<WebElement> itr = wlcmLinks.iterator();

    while(itr.hasNext() && (finalLink == null)) {
        WebElement link = itr.next();
        if (link.getText().equals(query)) {
           finalLink = link;
        }
    }

    if (finalLink == null) {
        throw new NoSuchElementException("No <a /> with that String");
    }

    return finalLink;

}

0
投票

试试这个。它可能会工作..

String helpFile = driver.findElement(By.xpath(“// td [1] // a [1] // * @ class ='wlcmlink']”))。getText();

String codeFile = driver.findElement(By.xpath(“// td [1] // a [2] // * @ class ='wlcmlink']”))。getText();

© www.soinside.com 2019 - 2024. All rights reserved.