-1

enter image description here

enter image description here

enter image description here

please send me the full code I tried Some XPath and CSS:

XPath:

//div[@id='autofill']/div/label 
//div[4]/div/label

CSS:

div.col-md-1.checkbox.checkbox-primary > label*

but using above locators I'm not able to click on the checkbox.

Anyone could suggest me on this, What needs to done?

1
  • All you examples show looking for label. But label is not checkbox. You cannot click, or get status of a label. You need to first locate the correct element. @Zhivko.Kostadinov has the correct path in their answer. – MivaScott May 3 '18 at 18:25
0

Does it work?

   if ( !driver.findElement(By.id("selectCheckBox")).isSelected() )
    {
         driver.findElement(By.id("selectCheckBox")).click();
    }

p.s In case you wish to select (if it's not selected) or if it's selected, code remove the check box. BUT: The problem is you can't locate an element, or you can't write the code with Java?

1
  • Please mention proper description why should it work & how it resolve the problem. – Nitin Rastogi May 3 '18 at 9:21
0

Use this code.

String demo = "//input[@id='selectCheckBox']";

if(!driver.findElementByXpath(demo).isSelected())    
// to check the checkbox is already selected or not

{

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElementByXpath(demo).click();   // Select the checkbox

}
0

Use explicit wait condition and preform click.

WebDriverWait wait = new WebDriverWait(driver, 10);   
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("selectCheckBox")));
element.click();

The above Java code states that we are waiting for an element for the time frame of 20 seconds as defined in the "WebDriverWait" class on the webpage until the "ExpectedConditions" are met and the condition is "visibilityofElementLocated".

0

When you get a ElementNotVisibleException, there can be multiple reasons.

  1. In UI check the view port of the element. Make sure the element is not overridden by any other element.
  2. Check if the element is present in the current browsing context.
  3. If the application supports localization, different browsers render the content in different way. Try locating the element in the same browser in which you are running the automation.
0

Please check with the below xpath,

driver.findElement(By.xpath(".//*[@id='selectCheckBox']/parent::*")).click();

If not working, please paste the html portion in the question. Not as an image. So that the locators can be checked.

0

There are plenty of things that might be going wrong for you , please try below mentioned solutions one by one as per the situation :

  1. Using Actions class: Link to Official documentation

    As the method documentation says,

Call perform() at the end of the method chain to actually perform the actions.

The general way to achieve click using Actions class is below :

actionsObj.moveToElement(element1).click().build().perform()
  1. If Actions class fails , sometimes the reason can be that you receive below exception :

    ElementNotInteractableException [object HTMLSpanElement] has no size and location

That can mean two things :

a. Element has not properly rendered: Solution for this is just to use implicit /explicit wait

  • Implicit wait :

    driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

  • Explicit wait :

    WebDriverWait wait=new WebDriverWait(driver, 20); element1 = wait.until(ExpectedConditions.elementToBeClickable(By.className("fa-stack-1x")));

b. Element has rendered but it is not in the visible part of the screen: Solution is just to scroll till the element. Based on the version of Selenium it can be handled in different ways but I will provide a solution that works in all versions :

    JavascriptExecutor executor = (JavascriptExecutor) driver;
    executor.executeScript("arguments[0].scrollIntoView(true);", element1);
  1. Suppose all this fails then another way is to again make use of Javascript executor as following :

    executor.executeScript("arguments[0].click();", element1);

  2. If you still can't click , then it could again mean two things :

1. Iframe

Check the DOM to see if the element you are inspecting lives in any frame. If that is true then you would need to switch to this frame before attempting any operation.

    driver.switchTo().frame("a077aa5e"); //switching the frame by ID
    System.out.println("********We are switching to the iframe*******");
    driver.findElement(By.xpath("html/body/a/img")).click();

2. New tab

If a new tab has opened up and the element exists on it then you again need to code something like below to switch to it before attempting operation.

String parent = driver.getWindowHandle();
driver.findElement(By.partialLinkText("Continue")).click();
Set<String> s = driver.getWindowHandles();
// Now iterate using Iterator
Iterator<String> I1 = s.iterator();
while (I1.hasNext()) {
String child_window = I1.next();
if (!parent.equals(child_window)) {
    driver.switchTo().window(child_window);
    element1.click() 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.