3

我正在尝试使用 selenium 查找网页的损坏图像/链接。我正在使用以下代码。问题是在 Eclipse 中e.getText()显示错误为“方法 getText 未定义类型 webelement”所以我也进行了类型转换,但它仍然显示新对象类型的相同错误。代码如下,

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++;
        }

我在这里错过了什么吗?

更新......这是我的进口,

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;

除了这个,我还需要导入任何特定的类吗?

4

1 回答 1

0

您的代码在我的机器上运行良好,我使用的是 Selenium 2.37.0 版。我相信getText()已经是每个 Selenium 版本的 WebElement 的预定义方法。您可以尝试通过转到Project -> Clean来清理您的项目,看看是否有帮助......

于 2013-11-18T18:25:02.983 回答