0

我将从先前操作选项 id 中的 html 文件中获取,并将它们存储到一个列表中。

然后为列表中的每个成员尝试打开页面lile:www.xxx.xxx/en/account/service/SERVICEID

对于第一个有效元素页面已打开,但随后浏览器将停止任何活动......我会得到这个

org.openqa.selenium.StaleElementReferenceException:在缓存中找不到元素 - 页面可能在查找后已更改 命令持续时间或超时:3.28 秒

   public class testy2{
private WebDriver driver;
private String baseUrl;
private List<WebElement> serviceList;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();

@BeforeClass
public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "https://www.fastgsm.com";

   // driver.manage().timeouts().pageLoadTimeout(120,TimeUnit.SECONDS);


}

@Test
public void test() throws Exception {
    //Open page and set up login
    driver.get(baseUrl + "/en/account/login");
    driver.findElement(By.id("login")).clear();
    driver.findElement(By.id("login")).sendKeys("selenium@account");
    driver.findElement(By.id("password")).clear();
    driver.findElement(By.id("password")).sendKeys("1Selenium");
    driver.findElement(By.id("password")).submit();


    serviceList = driver.findElements(By.xpath("//select[@name='token_id']/option"));


    for (WebElement service : serviceList) {

        if(!service.getAttribute("value").contains("-")){
        String test = service.getAttribute("value");
        System.out.println("Checking service name : " + service.getAttribute("value") + ",on page " + (baseUrl + "/en/account/service/"+service.getAttribute("value")) );

        driver.get(baseUrl + "/en/account/service/"+test);

        System.out.println("Service name : " + service.getAttribute("value") +"status : OK");

        }
        else{
        System.out.println("ERROR : INVALID SERVICE NAME");
        }
    }


}
4

1 回答 1

0

我不确定您使用的是什么语言,所以这将是伪代码:

serviceList = driver.findElements(By.xpath("//select[@name='token_id']/option"));

// create a string array of the count of elements in serviceList
private string[] serviceIDs = new string[serviceList.Count];
private int i = 0;

// populate serviceIDs with the attribute "value" from the elements in serviceList
for (WebElement service : serviceList) {
    serviceIDs[i++] = service.getAttribute("value");
}

for (serviceID : serviceIDs) {
    if(!serviceID.contains("-")){
    String test = serviceID;
    System.out.println("Checking service name : " + serviceID + ",on page " + (baseUrl + "/en/account/service/"+serviceID) );

    driver.get(baseUrl + "/en/account/service/"+test);

    System.out.println("Service name : " + serviceID +"status : OK");

    }
    else{
    System.out.println("ERROR : INVALID SERVICE NAME");
    }
}
于 2013-09-19T21:52:11.587 回答