简体   繁体   中英

Getting error for Webelement getText method Selenium WebDriver

I am trying to find broken images/links for a web page using selenium. I am using the following code. The problem is in Eclipse for e.getText() is showing error as "The method getText is undefined for the type webelement" So I did type cast also but still it shows same error for new object type as well. Code is as follows,

List<WebElement> linkElements = (List<WebElement>) driver.findElements(By.tagName("a"));

        String[] linkTexts = new String[linkElements.size()];
        int i = 0;
        for (WebElement e : linkElements) {

            linkTexts[i] = e.getText();
            i++;
        }

Am I missing something here ?

Updating..... here are my imports,

import java.io.IOException;
import java.net.URI;
import java.util.List; 
import java.util.concurrent.TimeUnit;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

IS there any specific class i need to import another than this?

Your code is running fine on my machine and I am using Selenium version 2.37.0. I believe getText() is already a predefined method for WebElement with every version of Selenium. You could probably try cleaning your project by going to Project -> Clean to see if that helps...

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