Error when clicking on element - java

I am not able to click on an element in selenium web driver getting error:
Cannot click on element (WARNING: The server did not provide any stack trace information)
This problem is only on IE and everything works fine on Firefox.
I used isDisplayed() function but it's not showing element. Maybe opacity of the element is zero?

If the element is not visible you cannot click on it through normal ways. You could execute a javascript script to click on it though.
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

It may be related to fact that object was not loaded yet, you can wait until button is displayed.
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("id"))).click();

Related

Can't click on Submit button, not working in automation testing, works manually

I have a standard form with 2 text boxes to fill and a submit button. The Submit button suppose to work after filling in the first mandatory text box.
It works manually, but when running on the automation infrastructure, the element doesn't get clicked.
The odd thing is that when debugging, the submit button is not clickable too, although it's not greyed out.
I tried the 3 classic methods:
Javascript:
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Actions:
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();
click:
element.click()
**Not working manually while in automation **
only when closing the form and creating a new one it works.
To click() on any clickable element ideally you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following solutions:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//elementXpath"))).click();
As an alternative, using Actions class:
new Actions(driver).moveToElement(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//elementXpath")))).click().build().perform();
Another alternative, using JavascriptExecutor:
((JavascriptExecutor)driver).executeScript("arguments[0].click();", new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//elementXpath"))));
According to your steps an replays , may be there is a hidden popup or something block your elements ,so use WebDriverWait not only clickable but try to elementToBeSelected() , invisibilityOfTheElementLocated() or
presenceOfAllElementsLocatedBy()
increase the waiting time to be able to detect the issues

Element is clickable when ran in debug mode, but ran normally throws ElementClickInterceptedException

I am trying to do web automation.
I am defining a pop-up menu containing a button defined with either xpath or css respectively as
XPath:-->: //button[contains(text(), 'Open Door')
CSS:-->: div.device-item.content.view-content > div.detail > div > button.btn.btn-primary.ng-star-inserted
While all is well, it throws
org.openqa.selenium.ElementClickInterceptedException: element click intercepted:
when I am debugging the test one step at a time, it
runs successfully by clicking the button, with out any
problem. But when I am running the test, it fails. I hope it is not a wait issue, as we apply check waiting for
the presence of the button and verify it exists and clickable.
I believe many would advice to use JavaScriptExecutor approach, but our framework has a problem of returning any web element as a custom object called "Element" which is neither Web Element nor sub class of it, but extends Object and implements an interface called IElement, so we can't use JavaScriptExecutor method since it needs Web Element form of the button which we want to click on.
If it works in debug it means the overlay disappear automatically. You can wait for it to vanish
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("[id^='device-card']")));
And in any case you can wait for the button to be clickable
button = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(), 'Open Door')")));
button.click();
It will look something like:
driver.executeScript("document.querySelector('button.btn').click()")
Just adjust the css
There are a couple of things you need to take care:
XPath //button[contains(text(), 'Open Door'): This xpath can be optimized further in either of the formats:
//button[text()='Open Door']
//button[contains(., 'Open Door')]
CSS: div.device-item.content.view-content > div.detail > div > button.btn.btn-primary.ng-star-inserted looks good however I strongly believe button.btn.btn-primary.ng-star-inserted would also work.
"...Executes successfully in debug mode...": As the browser client gets ample time to settle the webpage dynamics and the desired WebElement gets enough time to get interactive.
"...we amply check waiting for the presence of the button and verify it exists and clickable...": Is a myth as:
presenceOfElementLocated(): Is the expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.
where as:
elementToBeClickable(): Is the expectation for checking an element is visible and enabled such that you can click it.
As your usecase is to invoke click() on the element you need to use the ExpectedConditions as elementToBeClickable().
Example:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("css_element"))).click();
You can find a detailed discussion in What is the most efficient way to wait for a page element (xpath) to show up in Selenium Webdriver?
Finally, when using the Selenium java clients, of coarse there are methods from JavascriptExecutor but that should be the last resort.
Update
As you are still seeing the error ...ElementClickInterceptedException: element click intercepted..., you can add an additional step to induce WebDriverWait for the invisibility and you can use either of the following options:
invisibilityOf(WebElement element):
new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOf(overlapping_webelement));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("css_clickable_element"))).click();
invisibilityOfElementLocated(By locator):
new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("css_overlapping_element")));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("css_clickable_element"))).click();
You can find a relevant detailed discussion in Selenium invisibilityOf(element) method throwing NoSuchElementException + WebDriverWait.ignoring(NoSuchElementException.class) is not working
you can move the mouse to that element and then click on it, even I was facing this issue and this solution solved it
Actions clickOnBtn = new Actions(driver);
clickOnBtn .moveToElement("//button[contains(text(), 'Open Door')").click().build().perform;
this happens even when the element is not visible completely onscreen, in this case you can scroll to that element and then use the above code as shown below
JavascriptExecutor jse2 = (JavascriptExecutor)driver;
jse2.executeScript("arguments[0].scrollIntoView()", ele);
Actions clickOnBtn = new Actions(driver);
clickOnBtn .moveToElement("//button[contains(text(), 'Open Door')").click().build().perform;
click element using javascriptExecutor
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
This even happens sometimes with the way we selected the element, the xpath can be changed, we can try to use a parent tag rather than the button and try
you can define your own ExpectedCondition with click action:
public class SuccessfulClick implements ExpectedCondition<Boolean> {
private WebElement element;
public SuccessfulClick(WebElement element) { //WebElement element
this.element = element;
}
#Override
public Boolean apply(WebDriver driver) {
try {
element.click();
return true;
} catch (ElementClickInterceptedException | StaleElementReferenceException | NoSuchElementException e) {
return false;
}
}
}
and then use it:
wait10.until(elementToBeClickable(btn));
wait10.until(new SuccessfulClick(btn));

JAVA/Selenium - clicking the first button on the page works 50% of the time

I'm using Selenium to automate some UI clicking for a web app. One one of the pages I have several buttons leading to some details. They all have the same name, but I'm ok with clicking either one, so I'm just clicking the first one. But... sometimes it works and sometimes it doesn't.
WebElement DetailsButton = setPresentElementByXpath("//input[1][#type='button' and #value='Go to Details']");
DetailsButton.click();
I'm using setPresentElementByXpath to dynamically wait for the element.
private WebElement setPresentElementByXpath(String xpath) throws Exception {
WebElement myDynamicElement = (new WebDriverWait(driver, 15))
.until(ExpectedConditions.presenceOfElementLocated(By.xpath(xpath)));
return myDynamicElement;
}
What am I doing wrong?
EDIT: I forgot to mention where it fails. It goes through DetailsButton.click(); without issues, but then it fails on clicking the next thing, the screenshots and logs say that the page that was supposed to be displayed after clicking the button was not there, so I'm assuming the button is not clicked.
Page load speed variation and caching might be the issue. If the element not found exception is happening, use an Explicit Wait for it to be clickable via ExpectedConditions.ElementToBeClickable(By).
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.elementToBeClickable(By.id("myDynamicElement")));
If the exception "another element would receive the click" is happening you can click the element directly with the JavaScriptExecutor. Note: The JS action has no return value (if it worked or not) and does not wait for a page load if one happens after the click, like the Selenium .click() does.
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click()", element);

Xpath selector not working in IE but working fine in Chrome and Firefox

I am using following xpath to click on element using JSExecutor in Selenium webdriver. This works fine in Firefox and chrome but does not work in IE.
Any idea to make this work? After lot of trial and error I have made this work in FF and chrome and have come up with the following XPath.
//*[contains(#class,'ui-select-choices-row') or contains(#id,'ui-select-choices-row')]//*[text()='TextofMyElementToBeclicked'
Additional info: This is a Jquery drop down on an angularJS application. When the user clicks on the drop down //ul is loaded and i am using the above xpath (which is part of //ul) to select the element based on text (using Javascript executor click). I used JS executor because, click() function in selenium simply could not click on the drop down element.
I am clicking element using below.
WebElement element = driver.findElement(By.xpath("YourNumbersXpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
enter code here
I successfully tested your XPath with IE11, so it's not an issue related to IE. It's most likely a timing issue. First click on the drop button, then wait for the targeted element to appear and finally click on it:
WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.get("...");
// move the cursor to the menu Product
WebElement element = driver.findElement(By.xpath("drop down button")).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("drop down item"))).click();
IE11 seems to struggle with contains(#class and possibly also the contains(#id. Try using alternative solutions like starts-with.

Selenium Web Driver: not able to find the element on the 2nd page.

I am using Java and Firefox and Firebug
I am not able to locate the element on the second page. For example if I login to gmail then I am not able to locate and click on the sent items or any other button on the next page.
I tried with the xpath (both absolute and relative) but every time I am getting an error that element not found.
with the code I am successfully able to login but as soon as the page loads I get an error "Element not Found".
Please suggest any solution
Unless you are telling WebDriver to wait until the element on the 2nd page is loaded, WebDriver will simply try to click the element as soon as its able to run. This is bad because your element might not yet be loaded while WebDriver is already trying to click it... TIMEOUT mayhem ensues...
Try the following... use the WebDriverWait class to make WebDriver wait for the element on the page to be loaded before attempting to click it...:
WebDriverWait wait = new WebDriverWait(driver, 100);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("your xpath")));
element.click();
The '100' in WebDriverWait(driver, 100) is the maximum amount of seconds you want WebDriver to repeatedly attempt to locate the element before it times out...
I agree with the answer by CODEBLACK. Also you can go for Imlicit wait,which would make selenium wait implicitly for a given period of time.
Try following:-
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
You can specify time as per your convenience.
Best O Luck. . .!

Categories

Resources