Webdriver - Can't Click On element
Can't Click On element?
Webdriver identified the element correctly, but the element is hidden or disabled so it can't click on it. This is built in validation in webdriver aka Selenium2, this kind of validation was not there in Selenium1.
This kind of validation is good, but some time I am not sure why it throws the error "Can't Click On element".
I came across a scenario where elements are visible on the screen and I am able to select it, but for Webdriver element is not displayed. Run the following statements to check how Webdriver is considering the element attributes.
System.out.println(driver.findElement(By.xpath("//div[1]/img[@alt='Copy Criterion']")).isDisplayed());
System.out.println(driver.findElement(By.xpath("//div[1]/img[@alt='Copy Criterion']")).isEnabled());
These kind of issues occur when there are dynamic changes on the screen using JavaScript, due to some reason Webdriver is not able to update the with the new attributes.
If the element was having an id, I would have run the following statement to click using JavaScript.
script = "document.getElementById('ctl00_cpheRFx_btnCriteriaAdd').click();";
((JavascriptExecutor) driver).executeScript(script);
I will explain my issue using below screenshot. When user select from the drop down, boxes appear on the screen, need to select the icons(Pointed in the screen shot). When I click using following state it says "Can't Click On element".
driver.findElement(By.xpath("//div[1]/img[@alt='Copy Criterion']")).click();
When verified the properties
.isDisplayed() = false
isEnabled() = true
Then I came to the conclusion that I can't use webdriver to click it as the built in validation block the code.
So started working on the click action using JavaScript. Below code resolved my problem. Carefully change the class names and regular expression as per your html.
String Script = "";
Script += "var elements = document.getElementsByTagName('img');";
Script += "var regex = new RegExp('^Copy Criterion');";
Script += "for(i=0;i<elements.length;i++){";
Script += " if(elements[i].title.match(regex)){";
Script += " elements[i].click();";
Script += " }";
Script += "}";
((JavascriptExecutor) driver).executeScript(Script);
Now you got power to click any element on the screen.
Also read
Webdriver - Issues with Sendkeys & Click
Webdriver - Select list box items
--
Can't Click On element?
Webdriver identified the element correctly, but the element is hidden or disabled so it can't click on it. This is built in validation in webdriver aka Selenium2, this kind of validation was not there in Selenium1.
This kind of validation is good, but some time I am not sure why it throws the error "Can't Click On element".
I came across a scenario where elements are visible on the screen and I am able to select it, but for Webdriver element is not displayed. Run the following statements to check how Webdriver is considering the element attributes.
System.out.println(driver.findElement(By.xpath("//div[1]/img[@alt='Copy Criterion']")).isDisplayed());
System.out.println(driver.findElement(By.xpath("//div[1]/img[@alt='Copy Criterion']")).isEnabled());
These kind of issues occur when there are dynamic changes on the screen using JavaScript, due to some reason Webdriver is not able to update the with the new attributes.
If the element was having an id, I would have run the following statement to click using JavaScript.
script = "document.getElementById('ctl00_cpheRFx_btnCriteriaAdd').click();";
((JavascriptExecutor) driver).executeScript(script);
I will explain my issue using below screenshot. When user select from the drop down, boxes appear on the screen, need to select the icons(Pointed in the screen shot). When I click using following state it says "Can't Click On element".
driver.findElement(By.xpath("//div[1]/img[@alt='Copy Criterion']")).click();
When verified the properties
.isDisplayed() = false
isEnabled() = true
Then I came to the conclusion that I can't use webdriver to click it as the built in validation block the code.
So started working on the click action using JavaScript. Below code resolved my problem. Carefully change the class names and regular expression as per your html.
String Script = "";
Script += "var elements = document.getElementsByTagName('img');";
Script += "var regex = new RegExp('^Copy Criterion');";
Script += "for(i=0;i<elements.length;i++){";
Script += " if(elements[i].title.match(regex)){";
Script += " elements[i].click();";
Script += " }";
Script += "}";
((JavascriptExecutor) driver).executeScript(Script);
Now you got power to click any element on the screen.
Also read
Webdriver - Issues with Sendkeys & Click
Webdriver - Select list box items
--
You can also pass the web element to the javascript executor method, No need to get the element by the java script.
ReplyDeleteWebElement urElem =driver.findElement(By.xpath("//div[1]/img[@alt='Copy Criterion']"));
((JavascriptExecutor) driver).executeScript(arguments[0].click(), urElem);
Hi Bharath,
DeleteWhen I tried clicking on a checkbox in a website I got an error:
org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: Element cannot be scrolled into view:[object HTMLInputElement]
Command duration or timeout:
Any pointer on what the issue could be?
I am using Firefox 17 and selenium server 2.28 jar.