2016-11-22 80 views
0

如何使用selenium-java從javascipt彈出日曆中選擇一個日期。 日曆在新窗口中打開。下面的代碼被硬編碼爲選擇日期17/11/2016如何從JavaScript中彈出日曆中選擇一個日期在java-java

// Store the current window handle 
String winHandleBefore = driver.getWindowHandle(); 

// Perform the click operation that opens new window 
// Click on calendar icon 
driver.findElement(By.xpath(".//*[@id='frm']/table/tbody/tr[5]/td[2]/label/a/img")).click(); 

// Switch to new window opened 
for(String winHandle : driver.getWindowHandles()){ 
    driver.switchTo().window(winHandle); 
} 

// Perform the actions on new window 
// Click on date 17/11/2016 
driver.findElement(By.xpath("html/body/table/tbody/tr/td/table/tbody/tr[5]/td[5]/span/a")).click(); 


// Close the new window, if that window no more required 
driver.close(); 

// Switch back to original browser (first window) 
driver.switchTo().window(winHandleBefore); 

我要給日期字符串&從日曆自動選擇。我怎樣才能改變代碼的選擇作爲字符串的任何日期的常見功能。日曆圖片在這裏添加。

enter image description here

日曆的HTML代碼是

<table align="center" bgcolor="#333333" border="0" cellpadding="1" cellspacing="0"><tbody><tr><td> 
    <table border="0" cellpadding="3" cellspacing="1"><tbody> 
    <tr align="center" bgcolor="#cccccc"><td colspan="7"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr align="center" valign="middle"><td><span class="title"><b><a class="titlea" href="javascript:if(window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.moveYear) window.opener.moveYear(-1)">&nbsp;«</a>&nbsp;<a class="titlea" href="javascript:if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.prepMonth) window.opener.prepMonth(10)">‹&nbsp;</a></b></span></td><td width="90"><nobr><b><span class="title">November 2016</span></b></nobr></td><td><span class="title"><b><a class="titlea" href="javascript:if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.nextMonth) window.opener.nextMonth(10)">&nbsp;›</a>&nbsp;<a class="titlea" href="javascript:if(window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.moveYear) window.opener.moveYear(1)">»&nbsp;</a></b></span></td></tr></tbody></table></td></tr> 
    <tr align="center" bgcolor="#cccccc"><td width="15"><span class="days">S</span></td><td width="15"><span class="days">M</span></td><td width="15"><span class="days">T</span></td><td width="15"><span class="days">W</span></td><td width="15"><span class="days">T</span></td><td width="15"><span class="days">F</span></td><td width="15"><span class="days">S</span></td></tr> 
    <tr align="center" bgcolor="#ffffff"><td><span class="day">&nbsp;</span></td><td><span class="day">&nbsp;</span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(1)">1</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(2)">2</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(3)">3</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(4)">4</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(5)">5</a></span></td></tr> 
    <tr align="center" bgcolor="#ffffff"><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(6)">6</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(7)">7</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(8)">8</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(9)">9</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(10)">10</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(11)">11</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(12)">12</a></span></td></tr> 
    <tr align="center" bgcolor="#ffffff"><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(13)">13</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(14)">14</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(15)">15</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(16)">16</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(17)">17</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(18)">18</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(19)">19</a></span></td></tr> 
    <tr align="center" bgcolor="#ffffff"><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(20)">20</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(21)">21</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(22)">22</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(23)">23</a></span></td><td bgcolor="#333333"><span class="currentDay"><a class="currenta" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(24)">24</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(25)">25</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(26)">26</a></span></td></tr> 
    <tr align="center" bgcolor="#ffffff"><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(27)">27</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(28)">28</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(29)">29</a></span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.pickDate) window.opener.pickDate(30)">30</a></span></td><td><span class="day">&nbsp;</span></td><td><span class="day">&nbsp;</span></td><td><span class="day">&nbsp;</span></td></tr> 
    <tr align="center" bgcolor="#ffffff"><td><span class="day">&nbsp;</span></td><td><span class="day">&nbsp;</span></td><td><span class="day">&nbsp;</span></td><td><span class="day">&nbsp;</span></td><td><span class="day">&nbsp;</span></td><td><span class="day">&nbsp;</span></td><td><span class="day">&nbsp;</span></td></tr> 
    <tr bgcolor="#cccccc"><td colspan="7" align="center"><span class="foot"><b><a class="foota" href="javascript:if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.hideCal) window.opener.hideCal()">[close]</a>&nbsp;&nbsp;<a class="foota" href="javascript:if (window.opener &amp;&amp; !window.opener.closed &amp;&amp; window.opener.clearDate) window.opener.clearDate()">[clear]</a></b></span></td></tr> 
    </tbody></table> 
</td></tr></tbody></table> 
+1

顯示'HTML'代碼爲日曆。另外我建議你避免使用絕對的'XPath'。 – Andersson

+0

@Andersson我爲日曆添加了html代碼。雅,我不想使用絕對xpath – user123

回答

0

 前/後的符號,所以我需要把一個空間(在linktext的)/基於前後對&nbsp;HTML代碼

public void selectDate(WebDriver driver, String format){ 
    //1) Initialize variables 
    String curr[] = getCurrentDate("dd/MM/yyyy").split("/"); 
    String req[] = format.split("/"); 

    int currDate = Integer.parseInt(curr[0]); 
    int reqDate = Integer.parseInt(req[0]); 

    int currMonth = Integer.parseInt(curr[1]); 
    int reqMonth = Integer.parseInt(req[1]); 

    int currYr = Integer.parseInt(curr[2]); 
    int reqYr = Integer.parseInt(req[2]); 

    //2) Select required year 
    if (currYr > reqYr){ 
     for (int i=0;i<(currYr-reqYr);i++){ 
      //decrease year 
      driver.findElement(By.linkText(" «")).click(); 
     } 
    } else if (currYr < reqYr){ 
     for (int j=0;j<(reqYr-currYr);j++){ 
      //increase year 
      driver.findElement(By.linkText("» ")).click(); 
     } 
    } 

    //3) Select required month 
    if (currMonth > reqMonth){ 
     for (int i=0;i<(currMonth-reqMonth);i++){ 
      //decrease month 
      driver.findElement(By.linkText("‹ ")).click(); 
     } 
    } else if (currMonth < reqMonth){ 
     for (int j=0;j<(reqMonth-currMonth);j++){ 
      // increase month 
      driver.findElement(By.linkText(" ›")).click(); 
     } 
    } 

    //4) Select required date 
    driver.findElement(By.linkText(String.valueOf(reqDate))).click(); 
} 

public static String getCurrentDate(String format) 
{ 
    Calendar cal = Calendar.getInstance(); 
    Date date = cal.getTime(); 
    SimpleDateFormat sdf = new SimpleDateFormat(format); 
    return sdf.format(date); 
} 
1

請讓我知道是否下面的代碼選擇所需的日期爲您服務。我無法測試此代碼。此方法預計所需日期爲「dd/MM/yyyy」格式。

public static void selectDate(String format){ 

    //1) Initialize variables 
    String curr[] = getCurrentDate("dd/MM/yyyy").split("/"); 
    String req[] = format.split("/"); 

    int currDate = Integer.parseInt(curr[0]); 
    int reqDate = Integer.parseInt(req[0]); 

    int currMonth = Integer.parseInt(curr[1]); 
    int reqMonth = Integer.parseInt(req[1]); 

    int currYr = Integer.parseInt(curr[2]); 
    int reqYr = Integer.parseInt(req[2]); 

    //2) Select required year 
    if (currYr > reqYr){ 
     for (int i=0;i<(currYr-reqYr);i++){ 
      //decrease year 
      driver.findElement(By.xpath("//a[contains(@href, 'moveYear(-1)')]")).click(); 
     } 
    } else if (currYr < reqYr){ 
     for (int j=0;j<(reqYr-currYr);j++){ 
      //increase year 
      driver.findElement(By.xpath("//a[contains(@href, 'moveYear(1)')]")).click(); 
     } 
    } 

    //3) Select required month 
    if (currMonth > reqMonth){ 
     for (int i=0;i<(currMonth-reqMonth);i++){ 
      //decrease month 
      driver.findElement(By.xpath("//a[contains(@href, 'prepMonth')]")).click(); 
     } 
    } else if (currMonth < reqMonth){ 
     for (int j=0;j<(reqMonth-currMonth);j++){ 
      // increase month 
      driver.findElement(By.xpath("//a[contains(@href, 'nextMonth')]")).click(); 
     } 
    } 

    //4) Select required date 
    driver.findElement(By.linkText(String.valueOf(reqDate))).click(); 
} 

public static String getCurrentDate(String format) 
{ 
    Calendar cal = Calendar.getInstance(); 
    Date date = cal.getTime(); 
    SimpleDateFormat sdf = new SimpleDateFormat(format); 
    return sdf.format(date); 
} 
+0

沒有它的不工作..我給這個日期** 2016年11月30日**&得到錯誤'org.openqa.selenium.NoSuchElementException:無法找到元素:{「方法」 :「鏈接文本」,「選擇器」:「」「}'。 – user123

+0

直接使用文本識別鏈接似乎存在一些問題。我已更新上述代碼中的位置屬性。你能不能請再試一次,讓我知道,它是否有效? – Mahipal

+0

that also not working..got'org.openqa.selenium.NoSuchElementException:無法找到元素:{「method」:「xpath」,「selector」:「// a [contains(@href,'prepMonth')] 「}'。讓我再次檢查html代碼,我從螢火蟲複製 – user123

相關問題