I have a test where I need to click on a disabled button.
I am using the Actions class to do this.
When the user clicks on the button, an alert is generated.
Below is the code i have written:
Actions mouseActions = new Actions(driver);
mouseActions.moveToElement(driver.findElement(By.id("disabled_element_id"))).click().build().perform();
Then I try to switch to the alert I get exception:
Exception in thread "main" org.openqa.selenium.NoAlertPresentException: No alert is present.
You need to use JavaScriptExecutor for this task, WebDriver is not able to click on elements which are disabled or invisible. So try something like
JavascriptExecutor js = (JavascriptExecutor) webDriver;
js.executeScript("document.querySelector(\"button[id=yourButton]\").click()");
Selenium has been written to replicate user interaction, therefore will not allow interaction with disabled objects as a human would not be able to either.
you can either;
Replicate the process a user would do to enable a button.
Use JavaScript to enable or perform the interaction
Related
I am using selenium 3.9.1 and java to automate testing of a web application. The web application has some dynamic content based on pressing of a button for example. The page refreshes whenever this button is clicked. A java script runs on button click and updates the DOM I think. At this time, when I try to access the button (which is visible on the page), I get a staleElementReferenceException.
Does Selenium automatically reload the DOM once it is changed? I am relatively new to selenium. I have researched into this and I have tried to refresh the page using driver.navigate().Refresh() to try to see whether this will solve the problem. It does not solve the issue.
Any pointers will be deeply appreciated.
Since the page has been refreshed, the button reference you have is to the button on the old page that no longer exists.
I'd say you need to get a new reference to the button on the refreshed page (eg call FindElementById).
If the page is refreshed all the items in the DOM are now stale. What this means is that all items found before the button press will have to be found again. Any attempts to use those items will more than likely be treated with a stale element exception.
However, if the button click mearilly affects items on the page without having to ask the webserver to give you a new page you could interact with the old items.
You could do something like this:
public void SaveAndAgainClick() throws Exception{
try{
clicksaveButton(); //method to click save button
WebElement someValue = driver.findElement(By.xpath("(//input[#name='someValue'])[1]"));
someValue.click();
}catch (StaleElementException e){
WebElement someValue = driver.findElement(By.xpath("(//input[#name='someValue'])[1]");
someValue.click();
}
}
If findElement gets staleElementError while looking for (//input[#name='someValue'])[1] then it will again try one more time in the catch block and most certainly find the element and clicks on it. Your test will pass if you follow this approach.
Here are the answers to your questions :
A java script runs on button click and updates the DOM I think : If you inspect the HTML of the element through Development Tools / Inspect Element the element attributes will reveal it all.
Consider the following HTML :
<input value="Click me" onclick="alert('Click!')" type="button">
In the given HTML as per the onclick attribute of this element, if you invoke click() method on the WebElement, an alert would be generated. Similarly the onclick attribute may invoke a JavaScript or Ajax which may bring-in/phase-out new/old elements from the HTML DOM
At this time, when I try to access the button I get a staleElementReferenceException : In this case you should induce WebDriverWait for the WebElement to be interactive before attempting to interact with the element. Else you may face either of the following exceptions :
StaleElementReferenceException
WebDriverException
ElementNotInteractableException
InvalidElementStateException
Does Selenium automatically reload the DOM once it is changed? Short answer, Yes it does.
Refresh the page using driver.navigate().refresh() : No invoking driver.navigate().refresh() wouldn't be a optimum solution as it may not invoke the intended JavaScript or Ajax properly. Hence the intended WebElement may not be interactive in a optimum way.
I am using Selenium chrome webdriver with java, I need help dragging an element out of an iframe and onto another element in a different iframe. Has anyone done this before and if so could you post an example for me?
Here is what I've tried so far, also I have tried many other solutions:
WebElement Atom = driver.findElement(By.xpath("//*[text()='Circle']"));
driver.switchTo().defaultContent();
driver.switchTo().frame("treeNavigation");
WebElement Event = driver.findElement(By.xpath("//*[text()='Line']"));
// Create Actions object passing in a WebDriver object
Actions builder = new Actions(driver);
// Chain some calls together and call build
Action dragAndDrop = builder.clickAndHold(Atom)
.moveToElement(Event)
.release(Event)
.build();
// Perform the actions
dragAndDrop.perform();
Haven't done that yet, but I would like to try the following;
move to the source iframe, get the element, clickAndHold, build, perform. Then switch to default content, then switch to target iframe, moveToElement, release, build, perform.
Any sample test page would help. Thanks.
I'm trying to access a screen in an application, which appears when I do a mouse-hover over a tab and click on one of the options. I used Actions method to execute this using selenium. Here's my code:
element=driver.findElement(By.id("tab"));
Actions hoverover=new Actions(driver);
hoverover.moveToElement(element).moveToElement(driver.findElement(By.id("menu"))).click().build().perform();
When I login to the application and directly call this tab, I'm able to access this without any issue. But the problem occurs when I access this tab from a different screen in the application.
Whenever I access the hover-over page from a different page in the application, sometimes the page loads correctly but most of the time it fails and I recieve 'no such element' or 'stale element reference' error.
I'm really not sure how it is able to access the tab without any issue sometimes and how sometimes it is throwing errors. Please guide me here and let me know if there is anything else(any additional functions/or an alternative to Actions?) I can do so that the mouse-over click works all the time.
EDIT: I tried using both Explicit and Implicit waits and even thread.sleep as well, but in vain. In Chrome(only in chrome) when i do a manual screen refresh while it tries to access the tab, it works. But when I do the same in my code [driver.navigate().refresh()], it's not working!!
The stale element exception is probably occurring between when you set element and when you hoverover. Selenium does something like:
WebElement element = driver.findElement(By.id("tab"));
// "element" has been set
Actions hoverover=new Actions(driver);
// Between here and hoverover, "element" has changed on the DOM
hoverover.moveToElement(element).moveToElement(
driver.findElement(By.id("menu"))).click().build().perform();
// Uh-oh, what's "element?" Better throw an exception!
Try eliminating the element= line and moving driver.findElement to the inside of moveToElement().
Actions hoverover = new Actions(driver);
hoverover.moveToElement(driver.findElement(By.id("tab")))
.moveToElement(driver.findElement(By.id("menu"))).click().build().perform();
You could also try throwing in a WebDriverWait between hovering over tab and menu.
hoverover.moveToElement(driver.findElement(By.id("tab"))).build().perform();
new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("menu")));
hoverover.moveToElement(driver.findElement(By.id("menu"))).click().build().perform();
I am trying to click on "View Users" but for some reason it works with Chrome but not firefox.
//this clicks on the Admin tasks on the top.
driver.findElement(By.id("x-auto-34")).click();
//moves mouse over user profile management under the drop down menu for admin task
actions.moveToElement(driver.findElement(By.id("UserProfileManagement"))).build().perform();
//finds user configuration and clicks.
driver.findElement(By.id("ViewUsers")).click();
I tried this with xpath also but got the same issue only with firefox.
When i run it, it doesnt get any noelement found exception or anything.
This happens to me a lot when developing Selenium automation. Sometimes actions executed on a WebElement just fail to work. I use a Javascript workaround to execute clicks when the Selenium click method doesn't work:
WebElement element = driver.findElement(By.id("ViewUsers"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Trying to use the ieDriver.switchTo().window(windowHandle) method to switch to a popup window but my test script stops and does not proceed.
When I close the window manually i get the error
org.openqa.selenium.NoSuchWindowException: Unable to get browser
I know the window exists because I used the ieDriver.getWindowHandles() method to retrieve it.
All my protected mode settings are the same, I even tried to use the 'INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS' technique to no avail. Any other suggestions?
I'm running selenium 2.32.0 with IE9 on a Windows 7 machine.
Above code isfor handling window pops.
If you want to handle javascript popups like alerts, or confrmation popup, you need to use
driver.SwitchTo.alert().accept();
or
driver.SwitchTo.alert().dismiss();
hope it'll help you
maybe the popup is generated, with iframe, then you have to use switchTo.frame();
You should do something like:
WebDriverWait webDriverWait= new WebDriverWait(driver, 5000);
webDriverWait.until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
First you will initialize a WebDriverWait object that will allow you to wait until some condition is met, in this case - alert is present.
Then, the driver will be switched to this alert,