我将从以前的操作选项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");
}
}
}
答案 0 :(得分: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");
}
}