Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java selenium-如何在<;dv>;属性_Java_Selenium - Fatal编程技术网

Java selenium-如何在<;dv>;属性

Java selenium-如何在<;dv>;属性,java,selenium,Java,Selenium,我遇到的问题是,我不希望在单击17或18时指定绝对xpath,而是希望能够通过指定文本来单击。但是链接文本在html中查找或href属性,我有属性,我需要帮助了解如何通过指定文本来单击它 以下是驻留在表中的示例html代码: <td class="centerText day" data-bind="click: function(){$root.dayClicked($parent.days()[$index()])}, text: $parent.days()[$index()].da

我遇到的问题是,我不希望在单击17或18时指定绝对xpath,而是希望能够通过指定文本来单击。但是链接文本在html中查找
href
属性,我有
属性,我需要帮助了解如何通过指定文本来单击它

以下是驻留在表中的示例html代码:

<td class="centerText day" data-bind="click: function(){$root.dayClicked($parent.days()[$index()])}, text: $parent.days()[$index()].day, css: { currentMonth: !$parent.days()[$index()].currentMonth, currentDay: $parent.days()[$index()].currentDay, selectedDay: $parent.days()[$index()].selectedDay }">17</td>

<td class="centerText day" data-bind="click: function(){$root.dayClicked($parent.days()[$index()])}, text: $parent.days()[$index()].day, css: { currentMonth: !$parent.days()[$index()].currentMonth, currentDay: $parent.days()[$index()].currentDay, selectedDay: $parent.days()[$index()].selectedDay }">18</td>
17
18
以下是我的selenium代码:

            //click the calendar button
    driver.findElement(By.xpath("//*[@id='btnTradeDate']")).click();

             //go through the calendar table to navigate through each cell
     WebElement dateWidget = driver.findElement(By.id("transactionGridControls"));  
     List<WebElement> rows=dateWidget.findElements(By.tagName("tr"));  
      List<WebElement> columns=dateWidget.findElements(By.tagName("td"));  

      for (WebElement cell: rows){  
       //Select 17th Date   
       if (cell.getText().equals("17")){  
       cell.findElement(By.linkText("17")).click(); //need help on this 
       break;
//单击日历按钮
driver.findElement(By.xpath(“/*[@id='btnTradeDate']”)。单击();
//浏览日历表以浏览每个单元格
WebElement dateWidget=driver.findElement(By.id(“transactionGridControls”);
列表行=dateWidget.findElements(按.tagName(“tr”));
列表列=dateWidget.findElements(按.tagName(“td”));
对于(WebElement单元格:行){
//选择第17个日期
如果(cell.getText()等于(“17”){
cell.findElement(By.linkText(“17”))。单击();//需要此方面的帮助吗
打破

问题在于,您拥有的HTML不是锚定标记,而是一个td,在单击时就像锚定一样。您应该能够使用
单元格。click()
代替
单元格。findElement
,除非我遗漏了什么。您应该能够使用如下XPath:
By.XPath来识别正确的
元素:(“//*[id='transactionGridControls']///td[text()='17']”
。这可能会使您的代码更简单。正如@Richard所说,您应该能够单击它,即使它不是链接。请注意,只有在执行JS时才可以;您确定是这样吗?我非常确定是这样。我进行了编辑(当它没有被编辑锁定时)看到17和18都包含在
td
s中,其中有触发onclick的javascript。我检查并获得了无法定位元素异常:线程“main”org.openqa.selenium.NoSuchElementException中的异常:无法定位元素:{“method”:“xpath”,“selector”:“/*[id='transactionGridControls']//td[text()='17']"}