Is it possible to click anyway when getting a click exceptions in the selenium Chrome driver? - selenium

We've been build a site for quite a while and I am implementing UI UnitTesting.
It relies heavily on overlaying buttons with "proxies" to allow for more capabilities like dragging from one iframe into another. From a testing perspective, I want/need to "click" the button. It should click the proxy element overlaid on it. Then the test can continue. This works in IE and even Chrome, but when we went to test on the Build Server it failed with the error
threw exception: System.InvalidOperationException: unknown error:
Element is not clickable at point (265, 87). Other element would
receive the click: div class="v-iframe-proxy" style="width: 100%;
height: 100%;" /div
I am hoping there is a way to ignore this condition and click anyway. Any help would be awesome.

I believe that you have 3 options here:
1. Perform click using JavaScript like this:
((JavascriptExecutor) webdriver).executeScript("arguments[0].click()", elementToClick);
Try to click using new Actions(webdriver).click().build().perform();
Surround your click with try catch and continue execution.

I think that your UI tests work on headless browser in the server side. So you need to set width and height initially.
If you print size of the window, you'll see: 0,0
System.out.println(driver.manage().window().getSize());
So you can set window's size as 1024x600 (or whatever you want):
Dimension d = new Dimension(1024,600);
driver.manage().window().setSize(d);

Related

There is no frame but still there is an error element is not found

Below is my code, the line =>
driver.findElement(By.xpath("//*[#id=\"quote_password\"]")).sendKeys("password"); throws exception that element is not found
#Test
public void mytest()
{
System.setProperty("webdriver.chrome.driver","Drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://billing.scriptinglogic.net/index.php/sessions/login");
driver.findElement(By.xpath("//*[#id='email']")).sendKeys("email");
driver.findElement(By.xpath("//*[#id='password']")).sendKeys("password");
driver.findElement(By.xpath("/html/body/div/div/form/input")).click();
driver.findElement(By.xpath("//*[text()='Quotes']")).click();
driver.findElement(By.xpath("//*[text()='Create Quote']")).click();
driver.findElement(By.xpath("//*[#id=\"quote_password\"]")).sendKeys("password");
}
Quick and dirty solution:
WebDriverWait wait = new WebDriverWait(driver, 15, 100);
driver.get("http://billing.scriptinglogic.net/index.php/sessions/login");
driver.findElement(By.id("email")).sendKeys("<EMAIL>");
driver.findElement(By.id("password")).sendKeys("<PASSWORD>");
driver.findElement(By.name("btn_login")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[text()='Quotes']"))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".create-quote"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.id("quote_password"))).sendKeys("password");
Updated based on the credentials supplied in the comment below. I've tweaked the selectors to use ID, name and class where applicable. There is no need to use complex XPath locators when they aren't needed.
The explicit waits are required based on the way the site is working and I've added JeffC's suggestion of using an elementToBeClickable expected condition for the input element (I don't think it's really required in this instance though since the site doesn't seem to ever disable the input field, so a visibilityOfElementLocated expected condition is just as good really).
This solution is working for me in Chrome and Firefox in standard mode and Firefox in headless mode. It's not working in headless mode in Chrome because the screen size is smaller and when the screen width goes below 1000px the header changes and the text "Quotes" is never displayed. Below 767px the header is completely removed and you get a side menu. This means that the flow of the script needs to change slightly based on resolution.
I would suggest asking your developer to add an ID to the menu items, it will make it easier to locate them and use the site in its various states. The quick and dirty solution to this problem is ensure the browser is a certain size while the test runs, you can do this by setting the size in the first line of your script:
driver.manage().window().setSize(new Dimension(1024, 768));
When you do this it passes in Firefox and Chrome in standard and headless mode.
Note: The lines with an explicit wait that result in an element being clicked are anchor elements so there is no point waiting for the element to be clickable as the condition is always going to return true.

Selenium for VBA - element not found error

I have been using a code since long time , but recently there is a new banner up which is hiding the element that I am trying to click.
Attaching the snapshot of error. The only help I need is I need to click the hidden element( if the browser window is maximized the element is visible).
.
Please help me.
If what you have said is true then you can use the following to maximize the window before clicking:
driver.Window.Maximize
Other options include:
1) Removing the banner
2) Scrolling the element into view
Can't write anything decent for those last two as your code is an image and I don't have a full URL to test with. You also haven't included the relevant HTML.
The "div.container-fluid" element is blocking the button you are trying to click.
You could try some of the following (as being shown here Element MyElement is not clickable at point (x, y)... Other element would receive the click):
prolong the wait before the click
use javascript executor

Selenium WebDriver Explicit Wait intermittently not working

Good day everyone,
I need your help in this method. I have a web page that will have a loading screen UI when the page loads, and I'm waiting for it to finish before clicking a button.
Here is my code:
#Step("Go to Audit Inquiry Screen")
public void launchAuditInquiry(){
WebDriver webDriver = Driver.webDriver;
WebDriverWait wait = new WebDriverWait(webDriver, 10);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className("loading-container")));
WebElement auditInquiryBtn = webDriver.findElement(By.linkText("Audit Inquiry"));
auditInquiryBtn.click();
}
My issue is sometimes, this code works fine. It will wait for the loading ui div to be invisible before clicking the button. But sometimes it will produce this error:
Error Message: org.openqa.selenium.WebDriverException: unknown error: Element <a class="module-item" href="/audit/inquiry">...</a> is not clickable at point (822, 436). Other element would receive the click: <div class="loading-container" style="display: flex; opacity: 0.899842;">...</div>
I tried adding another explicit wait before clicking the button to be sure, like this:
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className("loading-container")));
WebElement auditInquiryBtn = webDriver.findElement(By.linkText("Audit Inquiry"));
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Audit Inquiry")));
auditInquiryBtn.click();
But it will sometime produce the same error above, and sometimes it will work fine.
I'm just confused on how to remediate the issue.
Thank you guys for the comments, especially this: Selenium Web Driver & Java. Element is not clickable at point (36, 72). Other element would receive the click
It was helpful, but some items there, I have already tried, but did not work as well. The part that I did check was one item there and also a comment here also which is to force a click:
new Actions(driver).moveToElement(auditInquiryBtn).click().perform();
But I'm having second thoughts on this because, a scenario may happen that when the loading container div is still overlaying the page, then I forced clicked the submit button, it will also produce another loading container div, and I'm not sure what will happen if there are two loading container div present.
Now, my solution on this is to adjust the sleep timer of the wait function:
WebDriverWait wait = new WebDriverWait(webDriver, 10, 2500L);
It now works because it gives the loader div time to generate before the first check of wait. 500 ms was a bit fast for the loader to render. I'm still testing this but if it didn't work, I might do the solution above.
Thanks again.
First thing to try is remove the invisibilityOfElementLocated wait and just use elementToBeClickable. I've never really trusted what Selenium considered "visible" and "invisible".
I've had issues in the past where the element to click on was completely off screen, so Selenium automatically scrolled until it was considered in the viewport. But because of a floating footer, it didn't scroll enough and was still behind the footer so could not be clicked on. It was still considered "visible" because it was in the viewport.
But, if you're sure, you can try forcing a click at a coordinate instead of an element.
new Actions(driver).moveToElement(auditInquiryBtn).click().perform();

How to send a click to hidden button?

I have a simple submit button on a page that I need to send a click for it but this button is at the bottom of the page and does not appear unless a user uses the browser vertical scroll bar to reach it !!
so when I use this C# code I get error element is not visible so how I can solve this problem ?
driver.FindElement(By.Id("submit")).Click();
You could scroll down using javascript. Something like this should do the work.
((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
Selenium Webdriver implicitly scroll to the element, if it is visible. You can wait for the visibility of the element and then perform the click operation. In Java, we can wait for the visibility using following code:
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(driver.findElement(By.id("submit"))));
In the above code, Webdriver will wait for 60 seconds, for the required element to become visible. If within 60 seconds, the element does not appear, then it will fail with timeout error. Once, the required element is visible, it can be clicked.
If it's not visible by you, it is not render by the browser used through your selenium driver.
I recommend you to simulate a scrolling to ensure your item will be visible, or do a simulation on a bigger resolution ?
Eventually you can consider switch to an headless driver...
I found the problem there are two elements in the page with id="submit" !! I used Name and now it work fine, thank to all.

Selenium-Webdriver (Java) failing to execute 'hoverover and click' function consistently

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();