WebElement click() not working in Selenium - 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.

Related

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.

How to resolve org.openqa.selenium.WebDriverException?

I am writing an automated test and want to report bugs, if occur, directly in the repo at GitHub. The step which fails in my program is the Submit new issue button from GitHub Issue Tracker.
Here is the code:
WebElement sendIssue = driver.findElement(By.xpath("/html/body/div[5]/div/div/div[2]/div[1]/div/form/div[2]/div[1]/div/div/div[3]/button"));
sendIssue.click();
And the exception:
org.openqa.selenium.WebDriverException: Element is not clickable at
point (883, 547.7999877929688). Other element would receive the click:
div class="modal-backdrop"></div
The following command also does not work:
((JavascriptExecutor) driver).executeScript("arguments[0].click();", sendIssue);
How can I make it clickable? Is there any other way by which I can resolve this issue?
This is happening because when selenium is trying to click ,the desired element is not clickable.
You have to make sure that the Xpath provided by you is absolutely right.If you are sure about the Xpath then try the following
replace
WebElement sendIssue = driver.findElement(By.xpath("/html/body/div[5]/div/div/div[2]/div[1]/div/form/div[2]/div[1]/div/div/div[3]/button"));
sendIssue.click();
with
WebElement sendIssue =(WebElement)new WebDriverWait(DRIVER,10).until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div[5]/div/div/div[2]/div[1]/div/form/div[2]/div[1]/div/div/div[3]/button")));
sendIssue.click();
If that doesn't work ,You will get an Timeout exception, In that case try incaresing the timeout amount from 10 to 20.
If it still doesn't work please post a screenshot of the HTML.
You need to write something in the issue title and description to make the issue clickable are you sure you are not making that mistake of clicking the button without writing anything in those places I am adding screenshot for your convenience.
Selenium Webdriver introduced in a previous version (v2.48) a new behavior that prevent clicks on elements that may be overlapped for something else (a fixed header or footer - for example) or may not be at your viewport (visible area of the webpage within the browser window).
You can see the debate here.
To solve this you will need to scroll (up or down) to the element you're trying to click.
One approach would be something like this post:
Page scroll up or down in Selenium WebDriver (Selenium 2) using java
Another, and maybe more reasonable, way to create a issue on Github, would be using their API. Maybe it would be good to check out!
Github API - Issues
Gook luck.
This worked for me. Instead of HTML browser this would be useful if we perform intended Web Browser
// Init chromedriver
String chromeDriverPath = "/Path/To/Chromedriver" ;
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors");
WebDriver driver = new ChromeDriver(options);

Selenium, what is the best practice to deal with element is not clickable

I am using selenium 2.46 (firefox driver) to develop an application. There are a lot of element.click() in my code. Sometimes that elements are not visible or not clickable make the application throws selenium exception.
To resolve that issue, i use WebdriverWait(driver, 10).until(...) for each single element which needs to be clicked.
My question is there is any other better way Or design pattern that can help me to solve the problem best.
Or at least i dont have to use WebdriverWait for each single element needs to be click().
You cannot avoid WebDriverWait. If you send a webdriver click command, webdriver will blindly assume that "element is clickable". You need to instruct webdriver to wait because your element is special and needs some synchronization before it can click on it. I don't think you need to do this for every other element. You can incorporate ExpectedConditions so that you can keep your code snippets manageable and small. So something like,
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.elementToBeClickable(By.id("foo"))).click();
The other option you can try other than clicking is hit enter on respective element, for that you can refer ID of that element.
driver.findElement(By.id("elementid")).sendKeys(Keys.ENTER);
use implicit wait instead of explicit wait and give the expected condition till the element doesn't visible on screen.
for more info you can check
http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#invisibilityOfElementLocated-org.openqa.selenium.By-
Hope this will help you

Mouse doubleclick not work in textbox using Selenium WebDriver

I try to double click on the text box by which the text will be selected.....The code looks like
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com/");
WebElement txtBoxElement=driver.findElement(By.xpath("//*[#id='email']"));
txtBoxElement.sendKeys("abc");
System.out.println("Test start");
Actions builder=new Actions(driver);
Action a=builder.moveToElement(txtBoxElement).doubleClick(txtBoxElement).build();
a.perform();
//This is for another way to double click on the text field
Locatable locatable = (Locatable) driver.findElement(By.name("email"));
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseMove(locatable.getCoordinates());
mouse.doubleClick(locatable.getCoordinates());
System.out.println("Test Complete");
}
but both of this way was not working I bypass this problem using this
Action a=builder.moveToElement(txtBoxElement).sendKeys(txtBoxElement,Keys.chord(Keys.CONTROL,"a")).build();
a.perform();
my question is why double click is not working?
I also try this for google search textbox it was also not working.
Here I want to mention one thing that I use Selenium 2.37 and firefox 26 .
I did not get any error but not double click on this element.I also observe that if I commented out the txtBoxElement.sendKeys("abc"); part and then sendkeys using Actions event,it write the text on browser address bar.
I believe you must be seeing following error while double clicking an element:
Cannot perform native interaction: Could not load native events component.
This is always issue with latest version of Firefox with latest verion of Selenium WebDriver(For more details please refer to link).
You can resolve this issue by downgrading Firefox from 26 to 25. With Firefox 25 your code will work.
It may be that you have the Action statement incorrect for the double-click. I did a double-click with simple statements like this:
Actions a = new Actions(driver);<br>
a.doubleClick(txtBoxElement);<br>
a.perform();
If you are trying to select all of the text in the text box try:
WaitUntil.elementReady(driver, 2,".//span[#class='scale-input-box']/input").sendKeys(Keys.CONTROL + "a");
To overcome problems with doubleclicking in Selenium try Alternative workaround Source
Simplified to this:
((JavascriptExecutor) driver).executeScript("document.getElementById('map_container').dispatchEvent(new Event('dblclick'));");

How to click Image icon using selenium webdriver

I am trying to click the image icon via xPath but when i run the code the link present on image icon is not opening. could you please help me in resolving this issue.
The Code i used to click the mail icon :
driver.findElement(By.xpath("//*[#id='e-switcher-mail-icon']")).click();
As slanec said more information is required or might be the element is not loaded. If u feel that the element has loaded and still its not happening, using java script is one way to click the image element.
Something like this
WebElement element = driver.findElement (By.xpath ("//*[#id='e-switcher-mail-icon']"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript ("arguments[0].click();" , element);
Could be that the element hasn't yet loaded in the DOM. Try waiting for expected conditions:
Wait<WebDriver> wait= new FluentWait<WebDriver>(driver).withTimeout(15L, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS);
WebElement icon = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#id='e-switcher-mail-icon']")));
icon.click();
Touché. It's the frames. Either <frame> or <iframe>, both need special care, see the documentation on the topic.
What you need to do:
driver.switchTo().frame("s_MainFrame");
after this, the driver's context will switch to the frame and all searches will be done in it, so you should be able to find the element without any further problems.
Once you're done in the frame and you need to switch back to the default context of the page, do:
driver.switchTo().defaultContent();