How to wait until page freezes in Selenium WebDriver? - selenium

We have got following logic on frontend - so when new page is loaded, screen is auto scrolling to specific section and I have a script which clicks on the specific element at that time. Hence, while this movement is occurring following error is appearing due to the fact that selenium is clicking on wrong element:
"Element is not clickable at point (x, y). Other element would receive
the click..."
I have added plain sleep, but this solution is not a good one. So does any one know how to wait until page freezes - when auto scrolling is finished?

f you know what element you're scrolling to (the element that is at the top of browser viewport when you're done with scrolling) you can wait for the y property of a navigator representing that element to equal zero. Following is an example that you can paste into groovy console which goes to a page and then scrolls to an element by using it's id in the url (I know there is no waiting here nor the scrolling is animated but I just want to show how that property can be used to achieve want you want):
I am not familiar with protractor, so you will have to adjust the block syntax appropriately.
waitFor {
elementWeScrollTo.y == 0
}

Related

Trouble getting an element to be clicked using Selenium

I am trying to go to the below URL
https://twitter.com/explore
Enter HBO Max in the text box which I locate using
By.cssSelector("input[placeholder='Search Twitter']")
and then I want to select the HBO Max option that appears in the dropdown. My locator for the element to be selected is
By.xpath("//div[#role='option']//li/div/div[2]")
Sometimes the element gets clicked and I go to the new page, sometimes not. I have in my framework waited for the element to be clickable using WebDriverWait. The element is both visible and enabled because I print these values before I click the element.
Additional debugging steps performed -
Click using JavaScript seems to have the same behavior.
I believe I am using the right locator because the mouse event gets generated for this element.
Thread.Sleep seems to work suggesting that perhaps a timing issue
Any inputs would be great.
try:
myElem = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, 'ID')))
print("Page is ready!")
except TimeoutException:
print("Loading took too much time!")
Try this !
what it does : It wait until the specific element of the site is loaded. If it takes more time than specified will just give you an output as "loading took too.........."
You can modify the time required to wait.

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 element is hidden behind floating header

The Selenium webdriver locator always puts elements on top of the page if scrolling is needed, but it wont take into account a floating header. At the moment I created a workaround with
Actions actions = new Actions(this.webdriver);
actions.sendKeys(Keys.ARROW_UP).perform();
Isnt there a nicer solution to tell the webdriver to center an element in the middle of the screen instead or with a fixed distance to the top?
The orange part is from the button the blue part is the header:
hidden button in orange
In my case, I choose to hide that floating element.
top = dr.find_element_by_xpath('//div[#node-type="top_all"]')
dr.execute_script("arguments[0].setAttribute('style','display:none')", top)
After that, move to the element I want, I can even add an offset.
Bring the element into view using javascript. Here is a possible solution:
Bring the element into the view port using selenium api.
Use javascript to figure out if the element is obstructed by another element, (floating header / footer etc...).
Use javascript to scroll to the middle of the view port.
See the javascript utility class I created here: https://github.com/gterre/stuff
usage...JavascriptUtils.bringIntoView(WebElement element);
I'm sure you can modify the script to your needs.
Selenium also has the ability to not scroll items to the top of the window before interacting with them (and therefore hiding them behind a floating header) but instead to the bottom.
This is achieved via the 'elementScrollBehavior' capability which can be set to 1 to scroll stuff to the bottom.
See this stack overflow for how it's done in capybara.
I solved the problem like this, so you just need to define in which cases your element is not in scope and add this method, it will automatically scroll it into view:
public void scroll_element_into_view(WebElementFacade element) {
int Y = (element.getLocation().getY() - 200);
JavascriptExecutor js = (JavascriptExecutor) getDriver();
js.executeScript("javascript:window.scrollTo(0," + Y + ");");}
Works for every element you can find by id, css, xpath or whatsoever.