简体   繁体   English

如何使用WebDriver和Java在日历表中找到链接?

[英]How to locate links in a calendar table using webdriver and java?

I'm learning to work with Maven, Java, testng and webdriver by doing a self training course. 我正在通过自学课程学习与Maven,Java,testng和webdriver一起工作。 I been doing great with all exercises until I got to the last one, I honestly don't know how to solve it. 在进行所有练习之前,我一直做得很好,直到我完成上一个练习为止,老实说,我不知道该如何解决。 Let me give you some context: 让我给你一些背景:

Exercise description : Open the site homepage and using the calendar(html calendar in a wordpress site), check how many days of the current month contain posts, then access the first day which has post and print the titles of the posts in the console. 练习说明 :打开站点主页,并使用日历(wordpress站点中的html日历),检查当月有多少天包含帖子,然后访问包含该帖子的第一天并在控制台中打印帖子的标题。

When any day in the calendar has posts, instead of showing the day in plain text, the site shows the day number as a link that once clicked opens the lists of posts in the main page. 当日历中的任何一天都有帖子时,该站点不会以纯文本形式显示日期,而是以链接的形式显示日期编号,单击该链接即可在主页上打开帖子列表。

This is a screen capture of how this site looks when I click on the day which has posts, in this case October 10th 2013: http://i.stack.imgur.com/wGRO2.jpg 这是当我单击有帖子的日期(在本例中为2013年10月10日)时该网站外观的屏幕截图: http : //i.stack.imgur.com/wGRO2.jpg

Like I said the site is a wordpress site (unfortunately can only be accessed through VPN so I can't share it), and I need to use the calendar widget which is a table. 就像我说的那样,该站​​点是wordpress站点(不幸的是只能通过VPN访问,所以我不能共享它),并且我需要使用日历小部件(即表格)。 I was able to go over each row and column of the table and print each column's text. 我能够遍历表格的每一行和每一列,并打印每一列的文本。

But how do I find if any of those days have a link in it and then having located that element how can I access it and print it's content (is this case they want me to print the titles of all posts created under that date)? 但是,如何查找那些日子中是否有任何链接,然后找到该元素,我该如何访问它并打印其内容(在这种情况下,他们是否希望我打印在该日期下创建的所有帖子的标题)?

This is the code I'm using to go over the entire table and print the content: 这是我用来遍历整个表格并打印内容的代码:

//Test Case 6
    @Test (timeOut=20000)
    public void TestCalendar(){
        HomePage homePage = PageFactory.initElements(driver, HomePage.class);
        homePage.Go(driver);
        WebElement table = homePage.ReturnCalendarElement();
        //Actual Month has no posts, so we select previous one which has 2 posts
        homePage.ClickPreviousMonth();
        //We Get All rows of the calendar then we get all columns and its data
        List<WebElement> rows = table.findElements(By.tagName("tr"));
        for (WebElement row: rows) {
            List<WebElement> cols= row.findElements(By.tagName("td"));
            for (WebElement col: cols){
                System.out.print(col.getText() + "\t");

            }
        }
    }

I'm adding the HTML code of that page in case it helps you to provide some answers: 我要添加该页面的HTML代码,以防它有助于您提供一些答案:

Follow this link to open a Public Gist with the HTML code of the page: https://gist.github.com/anonymous/599cd722b5c906808528 单击此链接可打开带有页面HTML代码的Public Gist: https//gist.github.com/anonymous/599cd722b5c906808528

The following is the HTML code for the Table I'm testing: 以下是我正在测试的表的HTML代码:

<aside id="calendar-2" class="widget widget_calendar"><h3 class="widget-title">Posts by date:</h3><div id="calendar_wrap"><table id="wp-calendar">
<caption>October 2013</caption>
<thead>
<tr>
    <th scope="col" title="Monday">M</th>
    <th scope="col" title="Tuesday">T</th>
    <th scope="col" title="Wednesday">W</th>
    <th scope="col" title="Thursday">T</th>
    <th scope="col" title="Friday">F</th>
    <th scope="col" title="Saturday">S</th>
    <th scope="col" title="Sunday">S</th>
</tr>
</thead>

<tfoot>
<tr>
    <td colspan="3" id="prev" class="pad">&nbsp;</td>
    <td class="pad">&nbsp;</td>
    <td colspan="3" id="next" class="pad">&nbsp;</td>
</tr>
</tfoot>

<tbody>
<tr>
    <td colspan="1" class="pad">&nbsp;</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
</tr>
<tr>
    <td>7</td>
    <td>8</td>
    <td>9</td>
    <td><a href="http://10.28.148.127/wordpress/2013/10/10/" title="What software testing is, It is used for:">10</a></td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
</tr>
<tr>
    <td>14</td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
</tr>
<tr>
    <td>21</td>
    <td>22</td>
    <td>23</td>
    <td>24</td>
    <td>25</td>
    <td>26</td>
    <td>27</td>
</tr>
<tr>
    <td>28</td>
    <td>29</td>
    <td>30</td>
    <td>31</td>
    <td class="pad" colspan="3">&nbsp;</td>
</tr>
</tbody>
</table></div></aside>

This is the last problem I need to solve to end with this training, so any help and orientation will be highly appreciated. 这是我在结束本培训后需要解决的最后一个问题,因此,我们将不胜感激任何帮助和指导。

Use a better CSS query to get the links of the calendar. 使用更好的CSS查询来获取日历的链接。

It would be something like: 就像这样:

td a

Elements TD with a link A inside. 元素TD中带有链接A。

Not much help without the HTML code of the page. 没有页面的HTML代码,没有太多帮助。

I ended up finding a solution, while i'm sure there should be a better way to do this. 我最终找到了解决方案,尽管我确定应该有更好的方法来做到这一点。 But this worked for me & might help someone else somehow: 但这对我有用,并可能以某种方式帮助其他人:

// Test Case 6
@Test(timeOut = 40000)
public void TestCalendar() {
    HomePage homePage = PageFactory.initElements(driver, HomePage.class);
    CalendarPage calendarPage = PageFactory.initElements(driver,
            CalendarPage.class);
    homePage.Go(driver); //this just navigates to the page
    homePage.ReturnPreviousMonth().click(); 
    //returns the calendar element mapped in the page
    WebElement table = homePage.ReturnCalendarElement();
    WebElement dayWithPost = calendarPage.GetFirstDayWithPosts(driver,
            table);
    Assert.assertNotNull(dayWithPost);
    System.out.println("We'll be working with the following day: " + dayWithPost.getText());
    dayWithPost.click();
    List<WebElement> articles = driver.findElements(By
            .className("entry-title"));
    for (WebElement article : articles) {
        System.out.println("Retrieved Post's Title: [ "
                + article.findElement(By.tagName("a")).getText() + " ]");

    }



public class CalendarPage {

public List<WebElement> ReturnArticlesOnPage(WebDriver driver) {
    List<WebElement> articles = driver.findElements(By.tagName("article"));
    return articles;
}

public WebElement GetFirstDayWithPosts(WebDriver driver, WebElement table) {

    List<WebElement> rows = table.findElements(By.tagName("tr"));
    for (WebElement row : rows) {
        List<WebElement> cols = row.findElements(By.tagName("td"));
        for (WebElement col : cols) {
            try {
                if (col.findElement(By.tagName("a")).getAttribute("href") != null ){
                    System.out.println("Day: " + col.getText()+ " has posts and was returned.");
                    return col;
                }

            }
            catch (NoSuchElementException e){
                //System.out.println("Day: " + col.getText()+ " has no posts.");
            }
        }

    }
    return null;
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM