简体   繁体   English

无法使用Java选择Selenium Webdriver中的复选框

[英]Not able to select a checkbox in Selenium Webdriver with java

I'm trying to select a checkbox but I'm not able to and didn't find any solution in other threads. 我正在尝试选择一个复选框,但我也无法在其他线程中找不到任何解决方案。

Here is my HTML code for drop down list: 这是我的HTML代码下拉列表: 在此处输入图片说明

code: @FindBy(css="input.ng-valid.ng-dirty.ng-touched") WebElement chrgAllocFee; 代码:@FindBy(css =“ input.ng-valid.ng-dirty.ng-touched”)WebElement chrgAllocFee;

chrgAllocFee.click(); chrgAllocFee.click();

but it is not working where as other checkboxes are working when I define and use in same way. 但是当我以相同的方式定义和使用其他复选框时,它不起作用。 I found that the difference as compared to others is that here html code has additional line with 我发现与其他代码相比,不同之处在于,此处的html代码与

Please suggest how to define so that it can be recognised. 请建议如何定义以便可以识别它。

Error getting: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"input.ng-valid.ng-dirty.ng-touched"} (Session info: chrome=69.0.3497.92) 错误得到: org.openqa.selenium.NoSuchElementException:否这样的元素:无法找到元素:{“方法”:“ css选择器”,“选择器”:“ input.ng-valid.ng-dirty.ng-touched”} (会议信息:chrome = 69.0.3497.92)

The desired element is an Angular element so you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions: 所需的元素是Angular元素,因此您必须诱使WebDriverWait使该元素可单击,并且可以使用以下解决方案之一:

  • cssSelector : cssSelector

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.ng-valid.ng-dirty.ng-touched[name='ChargeAllocationFee']"))).click(); 
  • xpath : xpath

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='ng-valid ng-dirty ng-touched' and @name='ChargeAllocationFee']"))).click(); 

I found solution: I used javascript for clicking that field and it works fine and here is the code: 我找到了解决方案:我使用javascript单击了该字段,它工作正常,这是代码:

JavascriptExecutor js = (JavascriptExecutor) driver; JavascriptExecutor js =(JavascriptExecutor)驱动程序;

js.executeScript("arguments[0].click();", CamWizOR.chrgAllocFee); js.executeScript(“ arguments [0] .click();”,CamWizOR.chrgAllocFee);

CamWizOR.chrgAllocFee -> WebElement name CamWizOR.chrgAllocFee-> WebElement名称

@FindBy(xpath="//input[@name='ChargeAllocationFee' and @type='checkbox']") @FindBy(xpath =“ // input [@ name ='ChargeAllocationFee'and @ type ='checkbox']”)

WebElement chrgAllocFee; Web元素chrgAllocFee;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM