我正在学习使用Maven、Java、testng和webdriver,通过做一个自我培训课程。在最后一次练习之前,我一直做得很好,我真的不知道该如何解决。让我给你一些背景:
练习描述:打开站点主页并使用日历( wordpress站点中的html日历),检查当前月中有多少天包含帖子,然后访问有帖子的第一天,并在控制台中打印帖子标题。 当日历中的任何一天都有帖子时,网站将日号显示为一个链接,一旦单击该链接,将打开主页中的帖子列表,而不是以纯文本显示日。
这是一个屏幕截图,当我点击有帖子的那一天时,这个站点是什么样子,在这个例子中是2013年10月10日:http://i.stack.imgur.com/wGRO2.jpg
就像我说的,这个站点是一个wordpress站点(不幸的是只能通过VPN访问,所以我不能共享它),我需要使用日历小部件,它是一个表。我能够遍历表的每一行和每一列,并打印每一列的文本。
,但是我如何找到那些日子中是否有一个链接,然后找到该元素,如何访问它并打印它的内容(这种情况下,他们希望我打印所有在该日期下创建的帖子的标题)?
下面是我用来遍历整个表并打印内容的代码:
//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");
}
}
}我正在添加该页面的HTML代码,以防它帮助您提供一些答案:
按照下面的链接打开带有页面:https://gist.github.com/anonymous/599cd722b5c906808528的HTML代码的公共Gist
下面是我正在测试的表的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"> </td>
<td class="pad"> </td>
<td colspan="3" id="next" class="pad"> </td>
</tr>
</tfoot>
<tbody>
<tr>
<td colspan="1" class="pad"> </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"> </td>
</tr>
</tbody>
</table></div></aside>--这是我最后需要解决的问题--以这次培训结束,所以任何帮助和指导都将受到高度赞赏。
发布于 2014-12-08 17:41:26
使用更好的CSS查询来获取日历的链接。
应该是这样的:
td a元素TD中有一个链接A。
没有页面的HTML代码帮助不大。
发布于 2014-12-10 21:31:12
我最终找到了一个解决方案,而我确信应该有一个更好的方法来做到这一点。但这对我有用&可能会对其他人有所帮助:
// 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;
}}
https://stackoverflow.com/questions/27362375
复制相似问题