[英]Selenium Webdriver loop control, infinite loop Java
我有以下for循環,但由於某種原因,我無法進入循環內的if語句。 我使用計數器來控制循環,但是由於某種原因,計數器從不從零開始,而似乎從隨機數開始。
是否有人對我的代碼有什么問題有任何想法。
public static void chk_DateAreInRange() throws Exception{
try
{
Date startDate = new Date();
Date endDate = new Date();
int abc = 0;
elementString = driver.findElement(By.id("ctl00_Content_gvList"));
final List<WebElement> dates = elementString.findElements(By.xpath(".//td[1]"));
do
{
for (WebElement date : dates)
{
System.out.println("inside dates for" +abc);
if(abc == 0)
{
System.out.println("inside if x=0");
String[] parsedString = date.getText().toString().split("\\-");// splits the webelement into a string from the - icon
Date ddd = formatter.parse(parsedString[0]);//gets the date we need from string - 12 Nov 2014
String startdateString = formatter.format(ddd);
startDate = formatter.parse(startdateString); // convert the string back into a date
}
if(abc == 99)
{
System.out.println("inside if x=99");
String[] parsedString = date.getText().toString().split("\\-");// splits the webelement into a string from the - icon
Date ddd = formatter.parse(parsedString[0]);//gets the date we need from string - 12 Nov 2014
String startdateString = formatter.format(ddd);
endDate = formatter.parse(startdateString); // convert the string back into a date
}
abc++;
}
}while(abc != 99);
if(getFromDateFull().before(startDate) || getToDateFull().after(endDate)){System.out.println("Date is not in range");}
}catch (Exception e){
Log.error("TOP dropdown is not found on the PLP View Schedule Page");
throw(e);
}
}
控制台輸出。
inside dates for349625
inside dates for349626
inside dates for349627
inside dates for349628
inside dates for349629
inside dates for349630
inside dates for349631
inside dates for349632
inside dates for349633
inside dates for349634
inside dates for349635
inside dates for349636
inside dates for349637
inside dates for349638
實際上,您從0開始,但是在第二個循環中執行abc ++,因此,如果您的List<WebElement> dates
包含99個以上的元素,則在內部循環完成時,您的abc大於99 ...因此
while(abc != 99);
永遠不要停止你的性格...
精度:您可以在List<WebElement> dates
擁有10個元素,但是如您所見
第二個循環開始,abc = 10,然后abc!= 99,所以繼續
下一個循環abc = 20,abc!= 99所以繼續
在10次循環后abc = 100和abc!= 99,所以一次又一次地繼續...
僅當List<WebElement> dates
包含1個元素,3個元素或11個元素時,您的代碼才有效;)
您應該將測試更改為:
}while(abc < 99);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.