我试试这个xpath,但我不知道如何从这里继续?
如何点击元素"第8天" ?
我正在使用selenium webdriver与java:
driver.findElement(By.xpath(//div[@class='dates']/div[@class='pointer']...);
当我添加"时,我不知道如何继续。 / div [' 8']"我收到错误
页面的html是:
<div id="monthlist" style="left: 0px; top: 0px;">
<div style="left: -1px; position: absolute;">
<div class="month" style="left: 0px;">
<div class="title">יוני 2016 </div>
<div class="weekdays_top"></div>
<div class="body">
<div class="weekdays">
<div class="dates">
<div class="invalid" style="border-bottom: 1px solid rgb(221, 221, 221);"></div>
<div class="invalid" style="border-bottom: 1px solid rgb(221, 221, 221);"></div>
<div class="invalid" style="border-bottom: 1px solid rgb(221, 221, 221);"></div>
<div class="invalid" style="border-bottom: 1px solid rgb(221, 221, 221);">1</div>
<div class="invalid" style="border-bottom: 1px solid rgb(221, 221, 221);">2</div>
<div class="invalid" style="border-bottom: 1px solid rgb(221, 221, 221);">3</div>
<div class="invalid" style="border-bottom: 1px solid rgb(221, 221, 221);">4</div>
<div class="pointer today" style="border-bottom: 1px solid rgb(221, 221, 221);">5</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">6</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">7</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">8</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">9</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">10</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">11</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">12</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">13</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">14</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">15</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">16</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">17</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">18</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">19</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">20</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">21</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">22</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">23</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">24</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">25</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">26</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">27</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">28</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">29</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">30</div>
</div>
</div>
<div class="bottom"></div>
</div>
<div style="left: 163px; position: absolute;">
<div class="month" style="left: 164px;">
<div class="title">יולי 2016 </div>
<div class="weekdays_top"></div>
<div class="body">
<div class="weekdays">
<div class="dates">
<div class="invalid" style="border-bottom: 1px solid rgb(221, 221, 221);"></div>
<div class="invalid" style="border-bottom: 1px solid rgb(221, 221, 221);"></div>
<div class="invalid" style="border-bottom: 1px solid rgb(221, 221, 221);"></div>
<div class="invalid" style="border-bottom: 1px solid rgb(221, 221, 221);"></div>
<div class="invalid" style="border-bottom: 1px solid rgb(221, 221, 221);"></div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">1</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">2</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">3</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">4</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">5</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">6</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">7</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">8</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">9</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">10</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">11</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">12</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">13</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">14</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">15</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">16</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">17</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">18</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">19</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">20</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">21</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">22</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">23</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">24</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">25</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">26</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">27</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">28</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">29</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">30</div>
<div class="pointer" style="border-bottom: 1px solid rgb(221, 221, 221);">31</div>
</div>
</div>
答案 0 :(得分:0)
如果所需元素的位置是固定的,则使用findElements()
并从列表中通过索引获取所需的元素:
WebElement desiredElement = driver.findElements(By.xpath("//div[@class='dates']/div[@class='pointer']").get(7);
我们使用7
,因为索引从0开始。
答案 1 :(得分:0)
调整答案@alecxe给出了上述......从示例中看,第8天并不总是出现在文档的同一位置,因此请尝试:
WebElement desiredElement = driver.findElements(
By.xpath("//div[@class='dates']/div[@class='pointer' and text()='8']")
);
答案 2 :(得分:0)
使用此:
driver.findElement(By.xpath(//div[@class='dates']/div[@class='pointer' and contains(text(),'8')]);