简体   繁体   中英

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: 在此处输入图片说明

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

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

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)

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:

  • cssSelector :

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.ng-valid.ng-dirty.ng-touched[name='ChargeAllocationFee']"))).click(); 
  • 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:

JavascriptExecutor js = (JavascriptExecutor) driver;

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

CamWizOR.chrgAllocFee -> WebElement name

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

WebElement chrgAllocFee;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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