How to select a value from a drop down menu - selenium

I want to select the option from the drop-down menu. I tried a number of ways but I failed.
I tried:
WebDriverWait wait = new WebDriverWait (driver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("iMacs")));
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("iMacs")));
waitForElementToBeDisplayed(driver.findElement(By.linkText("iMacs")), 200);
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='iMacs']")));
This is my code:
WebElement element = driver.findElement(By.linkText("Product Category"));
Actions action = new Actions(driver);
action.moveToElement(element).perform();
WebDriverWait wait = new WebDriverWait (driver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("iMacs")));
WebElement subElement = driver.findElement(By.linkText("iMacs"));
action.moveToElement(subElement);
action.click();
action.perform();
This is my error:
org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.linkText: iMacs (tried for 5 second(s) with 500 MILLISECONDS interval)

So you are hovering over some element and then trying to click on specific link? Which driver are you using? Do you actually see that this menu is correctly rendered after hover?
Your code fails on wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("iMacs")));
Try with wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("iMacs"))); instead and see if it fails then (and what exception it throws in that case).
And why are you using Actions API to perform click?

Please check the sendkeys option also. May be this help in your code.
driver.findElement(By.xpath("code']")).sendKeys("testdata");

I tried executing your code in Chrome and it always worked for me. However, as sometimes, it is failing for you, you can put recovery mechanism in your code, as shown below:
WebElement element = driver.findElement(By.linkText("Product Category"));
Actions action = new Actions(driver);
action.moveToElement(element).perform();
WebDriverWait wait = new WebDriverWait (driver, 5);
try {
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("iMacs")));
} catch (WebDriverException we) {
System.out.println("First attempt to wait for visibility of 'iMacs' failed. Retrying...");
action.moveToElement(driver.findElement(By.linkText("Product Category"))).perform();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("iMacs")));
}
WebElement subElement = driver.findElement(By.linkText("iMacs"));
action.moveToElement(subElement);
action.click();
action.perform();
Above code should always work. Please let me know, if you have any further queries.

Related

Selenium webDriver, is it possible for actions to run out of order?

Selenium webDriver, is it possible for actions to run out of order? For example,
WebElement buttonElement = ....
buttonElement.click();
WebElement anotherElement = ....
Actions actions = new Actions(driver);
actions.doubleClick(anotherElement ).perform();
Sometimes not always, The 2nd doubleClick action is executed before the first click(), which causes tests to fail.
Ideally that should not happen.
But you can improve your code by adding web driver wait.
You can initialize webdriverwait in this way :
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement buttonElement = ....
wait.until(ExpectedConditions.elementToBeClickable(buttonElement)).click();
For double click :
WebElement anotherElement = ....
Actions actions = new Actions(driver);
actions.doubleClick(wait.until(ExpectedConditions.elementToBeClickable(anotherElement ))).build().perform();
Try it out, this will make your test cases more stable.

wait is not working but thread.sleep is working, also not throwing exception in selenium

//Thread.sleep(300);
Actions action = new Actions(driver);
WebDriverWait wait3 = new WebDriverWait(driver, 60);
wait3.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[#id='toggleNav']/li[2]/a")));
WebElement we = driver.findElement(By.xpath(".//*[#id='toggleNav']/li[2]/a"));
action.moveToElement(we).perform();
WebDriverWait wait4 = new WebDriverWait(driver, 60);
wait4.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#id='toggleNav']/li[2]/ul/li[3]/a")));
WebElement we1 = driver.findElement(By.xpath("//*[#id='toggleNav']/li[2]/ul/li[3]/a"));
action.moveToElement(we1).click().build().perform();
With thread.sleep it works fine, but when used wait its not performing actions and also not throwing any element not found exceptions...
FYI used Java script too, in this case as well its not working when thread.sleep is commented
//Thread.sleep(300);
Actions action = new Actions(driver);
WebElement we = driver.findElement(By.xpath(".//*[#id='toggleNav']/li[2]/a"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", we);
// action.moveToElement(we).perform();
WebElement we1 = driver.findElement(By.xpath("//*[#id='toggleNav']/li[2]/ul/li[3]/a"));
JavascriptExecutor executor1 = (JavascriptExecutor)driver;
executor1.executeScript("arguments[0].click();", we1);
//action.moveToElement(we1).click().build().perform();
Instead of using
wait.until(ExpectedConditions.presenceOfElementLocated(By....)
use
wait.until(ExpectedConditions.elementToBeClickable(By....)
This way you are waiting for the found element to be clickable before proceeding.

while automating the slider, The slider moves only when i placed my mouse over it in selenium

http://automationpractice.com/index.php?id_category=5&controller=category#/
This is my code,whats wrong with it?
By locator = By.cssSelector(".ui-slider-handle.ui-state-default.ui-corner-all.ui-state-hover");
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
WebElement sliderRight = driver.findElement(locator);
action.dragAndDropBy(sliderRight,-40,0).build().perform();
action.moveToElement(sliderRight).click().build().perform();
Where's actions taking under consideration the driver.
Try to enter the following code,
Actions action=new Actions(WebDriver);
You have used a wrong cssSelector. I have modified your code and it should work now.
Here is the java code
System.setProperty("webdriver.chrome.driver", "src/chromedriver 3");
WebDriver driver = new ChromeDriver();
driver.get("http://automationpractice.com/index.php?id_category=5&controller=category#/");
Actions action = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver, 30);
//modified the css selector
By locator = By.cssSelector(".ui-slider-handle.ui-state-default.ui-corner-all");
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
WebElement sliderRight = driver.findElement(locator);
action.dragAndDropBy(sliderRight,70,0).build().perform();
action.moveToElement(sliderRight).click().build().perform();

Selenium Show A Custom Message Instead Of "TimeOut"

I'm writing a test code with Selenium. I'm waiting for an element like this:
WebDriverWait webDriverWait = new WebDriverWait(driver, 60);
webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Link Text")));
I need to show a custom message instead of time out message if expected element doesn't come.
I used withMessage() method bu is not working in this situation.
webDriverWait.withMessage("message");
How can I show a custom message instead of a time out message.
You need to catch the timeOutException and at that point provide your own custom error message. Something like:
try {
WebDriverWait webDriverWait = new WebDriverWait(driver, 60);
webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Link Text")));
} catch(TimeOutException e) {
System.out.println("[DEBUG] Could not find element in allowed time...")
}

getting Element is not clickable at point (355, 160) exception

My script is failing because of the following exception.
org.openqa.selenium.WebDriverException: unknown error: Element is not
clickable at point (355, 160)
While loading the page if the element appears in the background, selenium tries to click and fails. I have used webdriverwait. Out of 10 times it fails around 3 times minimum.
How can I avoid/handle this without using Thread.sleep();
You should wait until invisibility of element using invisibilityOfElementLocated as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath('xpath of please wait loading...')));
After this you could perform click on target element
Hope it will work..:)
Use explicit wait
WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("myElement")));
// or (new WebDriverWait(driver, 10)).until(
// ExpectedConditions.visibilityOfElementLocated(By.id("myElement")));
myElement .click();