简体   繁体   中英

Selenium WebDriver getText()

Using Selenium WebDriver .getText() function in Java, I'm trying to access some text without any positive results. Is not even in the WebElement object. The text I want to fetch is: "eNewsletter Sign Up"

The HTML code from where I am fetching this looks like this:

<a class="footer" href="/contact/email-sign-up/" onclick="aloneTracking('footerEmailSignUp')">eNewsletter Sign Up</a>

And the Java code looks like this:

     List<WebElement> idElemList = driver.findElements(By.className("footer"));

    System.out.println(idElemList.size());
    String str = "";
    if (idElemList.size() > 0) {
        WebElement firstElement = idElemList.get(0);
        str = firstElement.toString();
        System.out.println("**** : " + str);
        str = firstElement.getText();
        System.out.println("¨¨" + str);
    }

In the first System.out.println() I get that the list is nine elements long.

In the second:

<a onclick="aloneTracking('footerEmailSignUp')" href="/contact/email-sign-up/" class="footer">

In the third:

<a onclick="aloneTracking('footerEmailSignUp')" href="/contact/email-sign-up/" class="footer">

How can I get that text?

An easier solution would be to find the element by link text and call getText on that element

By.ByLinkText By.ByPartialLinkText

In the second and the third is the value of the String str after doing:

Second:

firstElement = idElemList.get(0);
str = firstElement.toString();
System.out.println("xxxx : " + str);

Result of System.out.println:

 xxxx : <a onclick="aloneTracking('footerEmailSignUp')" href="/contact/email-sign-up/" class="footer">

Third:

WebElement firstElement = idElemList.get(0);
str = firstElement.getText();
System.out.println("¨¨ : " + str);

Result of System.out.println:

¨¨ : <a onclick="aloneTracking('footerEmailSignUp')" href="/contact/email-sign-up/" class="footer">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM