如何使用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);
我要給日期字符串&從日曆自動選擇。我怎樣才能改變代碼的選擇作爲字符串的任何日期的常見功能。日曆圖片在這裏添加。
日曆的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 && !window.opener.closed && window.opener.moveYear) window.opener.moveYear(-1)"> «</a> <a class="titlea" href="javascript:if (window.opener && !window.opener.closed && window.opener.prepMonth) window.opener.prepMonth(10)">‹ </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 && !window.opener.closed && window.opener.nextMonth) window.opener.nextMonth(10)"> ›</a> <a class="titlea" href="javascript:if(window.opener && !window.opener.closed && window.opener.moveYear) window.opener.moveYear(1)">» </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"> </span></td><td><span class="day"> </span></td><td bgcolor="#ffffff"><span class="day"><a class="daya" href="javascript: if (window.opener && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && 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 && !window.opener.closed && window.opener.pickDate) window.opener.pickDate(30)">30</a></span></td><td><span class="day"> </span></td><td><span class="day"> </span></td><td><span class="day"> </span></td></tr>
<tr align="center" bgcolor="#ffffff"><td><span class="day"> </span></td><td><span class="day"> </span></td><td><span class="day"> </span></td><td><span class="day"> </span></td><td><span class="day"> </span></td><td><span class="day"> </span></td><td><span class="day"> </span></td></tr>
<tr bgcolor="#cccccc"><td colspan="7" align="center"><span class="foot"><b><a class="foota" href="javascript:if (window.opener && !window.opener.closed && window.opener.hideCal) window.opener.hideCal()">[close]</a> <a class="foota" href="javascript:if (window.opener && !window.opener.closed && window.opener.clearDate) window.opener.clearDate()">[clear]</a></b></span></td></tr>
</tbody></table>
</td></tr></tbody></table>
顯示'HTML'代碼爲日曆。另外我建議你避免使用絕對的'XPath'。 – Andersson
@Andersson我爲日曆添加了html代碼。雅,我不想使用絕對xpath – user123