Samy
Samy

Reputation: 73

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: enter image description here

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)

Upvotes: 0

Views: 994

Answers (2)

Samy
Samy

Reputation: 73

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;

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193088

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();
    

Upvotes: 1

Related Questions