如何从selenium-java

时间:2016-11-22 10:04:24

标签: javascript java selenium xpath calendar

如何使用selenium-java从javascipt弹出日历中选择日期。 日历将在新窗口中打开。以下代码经过硬编码以选择日期 17/11/2016

// 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>

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:0)

符号之前/之后有 ,因此我需要在&nbsp;代码中基于HTML之前/之后放置一个空格(在linktext中)

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);
}