I have below code to uncheck check box when it is selected, but when i run test i could see checkbox is unchecked for less than few seconds and again it is auto checked after less than few seconds. But I have no issues with Chrome and IE.
I am using Firefox Quantum(58.0.2 64-bit) and rememberMe is checkbox id.
if (driver.findElement(By.xpath("//*[#id='rememberMe']")).isSelected()) {
Thread.sleep(6000);
driver.findElement(By.xpath("//*[#id='rememberMe']")).click(); --> here it should uncheck check box.
Thread.sleep(6000);
}
I have used all other options like wait on element, send keys etc. Could some one please point me in right direction?
I would do something more like this
WebElement checkbox = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("rememberMe")));
if (checkbox.isSelected())
{
checkbox.click();
}
It waits for the element to be clickable then clicks it if it's selected.
Related
I'm trying to automate a site but stuck at the point where I need to complete the onboarding process of the user account.
So the problem is I have to add a user and to add the user I have to go through few steps, I have successfully added the user details but when I click on continue button it does not navigates me to the next on same page.
I want to know that how can I navigate to the next step on the same page button by clicking the continue button
Here is my code
public void enter_advisor_details() {
driver.findElement(user_mgmt_opt).click();
driver.findElement(advisor_tab).click();
driver.findElement(add_advisor_btn).click();
driver.findElement(first_name).sendKeys("Test");
driver.findElement(last_name).sendKeys("Automation");
driver.findElement(email).sendKeys("TestAuto#gmail.com");
WebElement element = driver.findElement(By.xpath("//div[#class='mt-8']//button"));
element.click();
}
Note: I have tried Actions class, WebDriverWait and JavaScript executor but nothing works for me
As you can see in the below image the test is getting passed and the button is clicked and the next step does not show up
enter image description here
You are probably missing a delay. Wait for element to become clickable before clicking it, as following:
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='mt-8']//button"))).click();
I need to close a dialog box that pops up after I had selected 'Save' by clicking 'Ok', however none of the elements on the page as well as dialog box can be inspected by right clicking after the pop-up (tried F12 doesn't help).
Alternatively, to close the dialog box enter key could be given, however I'm unable to send the enter key as listed below.
Actions action = new Actions(driver); //attempt 1
action.sendKeys(Keys.RETURN);
action.sendKeys(Keys.RETURN).perform(); //attempt 2
Both actions does not close the dialog box. I had performed the driver switch as well. In addition to the dialog box pop-up there's also an outlook email pop-up could this create an issue in identifying the alert pop-up? Kindly advise on how enter key could be passed to close the dialog box.
Found the issue, it was due to Thread.sleep(3000) that I had used after the click, guess this caused it to miss the alert from being recognized. I might be worng as well. Thanks for all your help!
As none of the elements can be inspected by right clicking after the pop-up which indicates its an Alert which is Javascript generated and you can use the following line of code :
Alert myAlert = new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
myAlert.accept();
Update A
As per the comment update :
Observation : In addition to the alert msg there's also outlook mail also pops up
Conclusion : If outlook mail pops up possibly it's not an Alert as suspected. You should be able to track the element within the HTML DOM.
Update B
As per the comment update :
Observation : there are 2 thing happens when i click on 'Save': [1]: Outlook email pop-up [2]: Alert notification on the saved page stating the records are being updated
Conclusion : Sounds like two window_handles opening up, treat them as window_handles.
Solution 1 : Try switching to the pop up and handle it.
new WebDriverWait(driver, 15).until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
Solution 2 : If you want to go with Keyboard keys.
Actions action = new Actions(driver);
new WebDriverWait(driver, 15).until(ExpectedConditions.alertIsPresent());
action.sendKeys(Keys.RETURN).perform();
In both cases, use 'wait' until you get an alert.
First, check the dialog box that pops up after you click 'Ok' is JavaScript Alert or anything else?
If the dialog is JavaScript Alert then try below code.
new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
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);
In my application I am not able to click on a partially visible check box, I even tried to do the same with javascript(scrool and click) and mouse hover actions, could any one find me a solution for the same?
You could try this:
WebElement g_element=g_driver.findElement(g_util.getObject("./ObjectRepository/"+objfile+".xml", objfile, office, "xpath"));
Webdriverwait wait = new Webdriverwait(driver,10)
wait.until(ExpectedConditions.elementToBeClickable(locator);
if(!elementChkBox.isSelected()) {
checkbox.click();
}
Having reviewed posts on this issue before, but problem persists.
http://preview.harriscountyfws.org/ is a public site, pertaining to this question.
I'm trying to click on a dropdown and select "Channel Status" from the Rainfall dropdown.
I get the following error:
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: element not visible: Element is not currently visible and may not be manipulated
I am attaching screenshot with code, but you may also visit the site and press F12 to look at the code.
Here is my current code based on research I have done so far:
Select dropdown = new Select(driver.findElement(By.id("siteType")));
WebElement triggerDropDown = driver.findElement(By.className("k-i-arrow-s"));
triggerDropDown.click();
dropdown.selectByVisibleText("Channel Status");
dropdown.selectByIndex(1);
Neither of the last two code statements shown work (dropdown.select...)
Both result in ElementNotVisibleException.
Well that's not true, because by pressing the triggerDropDown.Click(), the choices are visible!
Click Here For Screenshot
use the below code:
driver.get("http://preview.harriscountyfws.org/");
driver.manage().window().maximize();
Thread.sleep(2000);//use wait using until instead of this wait
WebElement elem = driver.findElement(By.xpath("//span[text() = 'Rainfall']"));
elem.click();
Thread.sleep(2000);
for(int i = 0; i <= 2; i++){//2 is used bacause u have 2 options
Actions actions = new Actions(driver);
actions.sendKeys(Keys.DOWN).build().perform();//press down arrow key
Actions actions2 = new Actions(driver);
actions2.sendKeys(Keys.ENTER).build().perform();//press enter
}
this will click on channel status button.
This is a strange one. I could click on the dropdown easily but clicking on "Channel Status" was not working. There's something about that dropdown that is not acting "normal". I tried the typical WebDriverWait but it doesn't work. Selenium is not waiting for it properly or something else is going on. I rarely recommend Thread.sleep() but in this case I can't figure out a way around it.
The code below works.
String searchText = "Channel Status";
driver.findElement(By.cssSelector("span.k-widget.k-dropdown.k-header")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//li[text()='" + searchText + "']")).click();