ElementNotVisibleException in Selenium - selenium

I have a problem with Selenium WebDriver throwing ElementNotVisibleException for the element being loaded in a pop-up window even though when instantiating the WebDriver I use:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Occurs for Chromedriver and IEDriver

Seems that solution was quite simple:
WebElement cBoxOverlay = wait.until(ExpectedConditions
.visibilityOf(driver.findElement(By.id("cboxOverlay"))));
Driver waits to load the cBox and then tries to find the close button

Related

WebElement click() not working in Selenium

I was learning automated selenium testing in https://www.yatra.com/etw-desktop/. While trying to click an image button named 'Asia'(image is attached) ,I am getting a Time out exception. Please help me in figuring out what's going wrong .
driver.manage().window().maximize();
driver.get("https://www.yatra.com/etw-desktop/");
driver.manage().timeouts().implicitlyWait(4000, TimeUnit.MILLISECONDS);
Thread.sleep(5000);
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable
(By.xpath("//*[#id=\"scrollable1\"]/div[1]/div/a[2]/div[4]"))).click();
Thread.sleep(4000);
Assert.assertEquals("https://www.yatra.com/etw-desktop/city-list",driver.getCurrentUrl());
image showing the web element to be clicked
Try this:
How to Find Web Elements in Shadow DOMs using Selenium
It is most probably because of using Thread.sleep(), which is the worst case of explicit wait. Instead use the wait method provided by selenium itself.

NoSuchWindowException - Selenium 3.4.0 - IE 11

I have launched IE 11 browser,
I have navigated to a initial URL --> done mouse over and clicked a link --> it redirects to another page.
In that page, I have to click a button, but it is throwing an exception
org.openqa.selenium.NoSuchWindowException: Unable to find element on closed window
but the windows still available on screen.
This is my code
WebElement e = driver.findElement(By.xpath("html/body/div[2]/div/div/header/nav/ul/li[2]/a"));
Actions a = new Actions(driver);
a.moveToElement(e).build().perform();
driver.findElement(By.xpath("//*[#id='menu-item-35']/a")).click();
TimeUnit.SECONDS.sleep(5);
// Exception is occurs after this, but when I delete the below code, the test case passes
driver.findElement(By.xpath("//*[#id='default_products_page_container']/div[3]/div[2]/form/div[2]/div[1]/span/input")).click();
This is the URL of the page: http://store.demoqa.com/
It looks to me like this is a race-condition error. I had those cases myself where I could actually see a window, but Selenium still said it wasn't there.
Have you tried setting the sleep time to a higher value?
You could also try to put a expect-wait-condition before clicking your element.
WebDriverWait wait = new WebDriverWait(DRIVER, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id='default_products_page_container']/div[3]/div[2]/form/div[2]/div[1]/span/input"))
Here is the Solution to your Question:
A few words about the solution:
The xpath html/body/div[2]/div/div/header/nav/ul/li[2]/a looks petty vulnerable to me use linkText locator.
Once you use Action Class to build().perform(), induce a bit of wait before locating another element.
Instead of xpath locator of //*[#id='menu-item-35']/a element, use linkText locator.
Again the xpath //*[#id='default_products_page_container']/div[3]/div[2]/form/div[2]/div[1]/span/input looks petty vulnerable to me use a logical uniquexpath.
Here is your own working code block with some simple tweaks in it:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement e = driver.findElement(By.linkText("Product Category"));
Actions a = new Actions(driver);
a.moveToElement(e).build().perform();
driver.findElement(By.linkText("iMacs")).click();
driver.findElement(By.xpath("//input[#name='Buy']")).click();
Let me know if this Answers your Question.

Switching from FireFoxDriver to ChromeDriver cause some element loading errors (Selenium)

I have a selenium project and until now I used FireFoxDriver, but now I tried to use ChromeDriver (which is X100 faster) and I get allot of fails in my tests with the error "element not visible" or "Element is not currently intractable and may not be manipulated"
Is there a need for more wait.until? Why is this?
Selenium needs elements to be visible/exist in the DOM in order to interact with them. As you said, Chrome is faster than FireFox, so the WebDriver is trying to interact with the DOM before when elements are not yet visible/exist.
Explicit wait
wait.until(ExpectedConditions.visibilityOfElementLocated());
And implicit wait
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Should solve your problem.

Selenium Webdriver: IEdriver getwindowhandles() method returns only 1 handle

I have recently started learning selenium. I am trying to automate a web portal. I have a button in webpage which opens another webpage. I am not able to switch to the newly opened window since getwindowHandle() is not returning the handle to newly opened window ( getwindowHandle.size();) returns 1 even the new window is open. I have given sufficient time for new page to load)
I googled this issue and one of the possible root causes I found was- protected mode not enabled in IE. Security settings in our company are controlled by group policy hence It's very hard for me to get this changed.
I wanted to check if there is any workaround for this issue.
Here is the code
WebDriver Driver= null;
System.setProperty("webdriver.ie.driver","M:\\Drivers\\IEDriverServer.exe");
Driver= new InternetExplorerDriver();
JavascriptExecutor jes= (JavascriptExecutor) Driver;
Driver.get("URL");
Thread.sleep(10000);
Driver.findElement(By.xpath("//*[#id='username']")).sendKeys("Username");
Driver.findElement(By.xpath(".//*[#id='password']")).sendKeys("Password");
Driver.findElement(By.xpath(".//*[#id='Login']")).click();
Thread.sleep(5000);
java.util.Set<String> handles= Driver.getWindowHandles();
// Check number of handles before clicking on the link.No. of handles
//returned=1
System.out.println(handles.size());
Driver.findElement(By.linkText("Cases")).click();
//Clicking on TestLink will open a new webpage
WebElement element = Driver.findElement(By.linkText("TestLink"));
element.click();
Thread.sleep(5000);
//Check number of handles after clicking on link.clicking on the link.No. of
//handles returned=1.
// Number of windows open at this point are 2
handles= Driver.getWindowHandles();
System.out.println(handles.size());
Driver.close();
Following is the configuration
OS: Windows 7 32 bit
Selenium: v2.46 ( Putting correct selenium version)
IEDriver- v2.46
Internet Explorer: IE9

Automation testing using selenium WebDriver?

Selenium RC command selenium.waitForPageToLoad("30000") is not working in WebDriver.
Is there any alternate command for this in WebDriver?
There are two types of waits you can use in Selenium; implicit and explicit.
Below examples are written in Java:
1) Explicit Wait:
new WebDriverWait(super.getDriver(), 10).until(ExpectedConditions.elementToBeClickable(site_logo));
Above code will wait 10 seconds for site logo element to be clickable, if not it will throw an exception. ExpectedConditions class has bunch of other methods you can use. You can check whether an element is present or not etc...
2) Implicit Wait:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
There is also Thread.sleep(Time in milliseconds); method, but I don't recommend you to use this one.
For more information: http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp
You can use WebDriveWait to solve it:
http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp
For waiting to an element, use wait.until(ExpectedConditions.visibilityOfElementLocated) :
#Test
public void test1() throws Exception {
WebDriverWait wait = new WebDriverWait(driver, 1);
driver.get("example.html");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(elementLocation)));
driver.close();
}