问题
Html code:
<table id="tblusref" style="width: auto;" onmouseover="UsortBinders()" role="grid">
<thead>
<tbody aria-live="polite" aria-relevant="all">
<tr class="ng-scope AssociateHighlight odd" >
<td class="ng-binding" style="width: 10px">1</td>
<td class=" text-center" style="width: 2px">
<input class="ng-pristine ng-untouched ng-valid" type="checkbox" ng-change="CheckChange(Ref.RecordNo,Ref.Freeze,Ref.isSelected,'US')" ng-model="Ref.isSelected">
</td>
</tr>
</tbody>
</table>
inside the table each and every row have an checkbox and i want to check the checkbox with particular row,and below is my xpath
WebElement checkBoxSelection = driver.findElement(By.xpath("//*[@id='tblusref']/tbody/tr[1]/td[2]/input"));
checkBoxSelection.click();
Please clarify
but during the execution
Firefox - its working correctly but Chrome and IE not checked
Firefox driver: 2.51.0
Chrome driver: 2.53.1
IE driver: 2.42.2
回答1:
Given element is not valid and also not able to find in console as well.
Please change element as per under of checkbox (Use one by one and keep whichever is proffered and working for you):
WebElement checkBoxSelection = driver.findElement(By.xpath("//input[@class='ng-pristine ng-untouched ng-valid']"));WebElement checkBoxSelection = driver.findElement(By.xpath("//input[contains(@class,'ng-valid')]"));Here change internal class text as you get unique element.WebElement checkBoxSelection = driver.findElement(By.cssSelector("input[class='ng-pristine ng-untouched ng-valid']"));
Then click on it.
checkBoxSelection.click();
As discuss in comment , There is one more scenario I want to add. There may be list of web elements at same level of xpath. Please use index for it.
driver.findElement(By.xpath("(//input[@class='ng-pristine ng-untouched ng-valid'])[1]"));
driver.findElement(By.xpath("(//input[@class='ng-pristine ng-untouched ng-valid'])[2]"));
And so on...
For particular this table the solution is :
xpath : (//table[@id='tblusref']//input[@class='ng-pristine ng-untouched ng-valid'])[1]
Please let me know If any query.
来源:https://stackoverflow.com/questions/41115126/ng-model-check-the-checkbox-xpath-not-working-in-chrome-and-ie-driver