我正在用Cucumber和Selenium编写一些自动化测试。目前,我正在尝试编写一个函数来选择日历选择器中的传递日期。当弹出时,我需要使用日历中显示的当前年份来确定某些逻辑。
但是,我不能为我的生活找出正确的语法,以便将当前年份的值转化为Java以进一步使用它。第一个div是可靠的非模糊标记,通过它我可以导航到我需要使用的日历,我甚至可以在浏览器开发控制台中以某种方式选择年份(在本例中为#34; 2017"),但我无法将其传递给Java。
相关HTML:
$table = new Table($output);
$table
->setHeaders(array('Col A', 'Col B', 'Col C'))
->setRows(array(
array('Row A', 'Divine Comedy', 'Dante Alighieri'),
array('Row B', 'A Tale of Two Cities', 'Charles Dickens'),
array('Row C', 'The Lord of the Rings', 'J. R. R. Tolkien'),
))
;
$table->render();

提取年份的最新尝试:
<ib-selector class="float-rg fblock40" ng-if="!hideYearSelector" initial-label="dateYear" next-handler="nextYear()" prev-handler="prevYear()">
<div class="calendar-month-year">
<span class="arrow-cal-left" ng-click="prevHandler()" role="button" tabindex="0">
</span> 2017
<span class="arrow-cal-right" ng-click="nextHandler()" role="button" tabindex="0">
</span>
</div>
</ib-selector>
它产生的异常:
String currentYear = driver.findElement(By.xpath("//ib-selector[@initial-label='dateYear']/div/text()")).getAttribute("data");
答案 0 :(得分:2)
尝试删除xpath表达式中的“/ text()”部分。
答案 1 :(得分:2)
使用xpath(假设你的路径没问题,因为exmple中的HTML实际上并不相关):
String text = driver.findElement(By.xpath("//ib-selector[@initial-label='dateYear']/div")).getText()
或者如果你想使用元素:
WebElement el = driver.findElement(By.xPath("//ib-selector[@initial-label='dateYear']/div"));
String text = el.getText();
这里是与示例中的HTML相关的路径:
String text = driver.findElement(By.cssSelector(".calendar-month-year")).getText()