我正在通过 self 培训类(class)学习如何使用 Maven、Java、testng 和 webdriver。我在所有练习中都做得很好,直到完成最后一个练习,老实说我不知道如何解决它。让我给你一些背景信息:
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.
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.
这是当我点击有帖子的日期(在本例中为 2013 年 10 月 10 日)时该网站的外观的屏幕截图:/image/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 代码,以便它可以帮助您提供一些答案:
点击此链接可打开包含页面 HTML 代码的公共(public)要点:https://gist.github.com/anonymous/599cd722b5c906808528
以下是我正在测试的表的 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>
这是我在本次培训结束时需要解决的最后一个问题,因此我们将非常感谢任何帮助和指导。
最佳答案
使用更好的 CSS 查询来获取日历的链接。
它会是这样的:
td a
元素 TD,内部有链接 A。
如果没有页面的 HTML 代码,没有多大帮助。
关于java - 如何使用 webdriver 和 java 查找日历表中的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27362375/