Webdriver do not load scroll - selenium

I have a problem that is occuring some times.
The page is not showing the scroll, so when I need to find/click an element that is not shown in the screen, I get an exception:
(org.openqa.selenium.ElementNotVisibleException: element not visible
Refreshing the page works, but I would like to know if there is other solutions for this issue.

You can simulate scroll using moveToElement from Actions class
// Java syntax, but similar to other languages
Actions actions = new Actions(driver);
WebElement elementToMoveTo = driver.findElement(...);
actions.moveToElement(elementToMoveTo).perform();

Related

Selenium WebDriver -- Unable to click on hyperlink, click goes to the other element

I am unable to click on Website hyperlink, the click goes to Recently used pages.
Tried with CSS locator of Website icon [which works in the lower environment as it doesn't have Recently used pages] reference in it.
Tried with XPath Locator[including custom XPath], still, the click goes to another item.
Tried name locator.
Used Actions class for clicking.
Allowed the page to load completely by using sleep and WebDriver wait.
Located the element and send Enter keys, still Recently used pages is clicked.
Tried to click it using coordinates.
Thought of ChromeDriver issue but the issue persists in Firefox too.
Tried below XPath:
html/body/div/div[2]/div[2]/div[1]/a/div
//div[2]/div/a/div
Code snippet:
WebElement elementToClick = driver.findElement(By.cssSelector(".icon.siteadmin"));
elementToClick.click();
WebElement elementToClick = driver.findElement(By.cssSelector(".icon.siteadmin"));
(JavascriptExecutor)driver).executeScript("window.scrollTo(0,"+elementToClick.getLocation().x+")");
elementToClick.click();
WebElement elementToClick = driver.findElement(By.cssSelector(".icon.siteadmin"));
Actions actions = new Actions(driver);
actions.moveToElement(elementToClick);
actions.click().perform();
Actions builder = new Actions(driver);
builder.moveToElement(elementToClick, 40, 207).click().build().perform();
Result: It clicks on Recently Used Pages, and it yields a result of Recently used pages instead of Website.
UI Reference
Development Code Snippet
Hope It Helps you:
.//div[#id='box_2']/a/div[#class='icon siteadmin']/div[1]
Try the following:
driver.findElement(By.XPath(“//a[contains(#title, ‘Websites’)]”)).click()
If this doesn’t work use the above XPath in combination with one of the moves to element paths above and then use click.

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: how to drag element to another iframe?

I am using Selenium chrome webdriver with java, I need help dragging an element out of an iframe and onto another element in a different iframe. Has anyone done this before and if so could you post an example for me?
Here is what I've tried so far, also I have tried many other solutions:
WebElement Atom = driver.findElement(By.xpath("//*[text()='Circle']"));
driver.switchTo().defaultContent();
driver.switchTo().frame("treeNavigation");
WebElement Event = driver.findElement(By.xpath("//*[text()='Line']"));
// Create Actions object passing in a WebDriver object
Actions builder = new Actions(driver);
// Chain some calls together and call build
Action dragAndDrop = builder.clickAndHold(Atom)
.moveToElement(Event)
.release(Event)
.build();
// Perform the actions
dragAndDrop.perform();
Haven't done that yet, but I would like to try the following;
move to the source iframe, get the element, clickAndHold, build, perform. Then switch to default content, then switch to target iframe, moveToElement, release, build, perform.
Any sample test page would help. Thanks.

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

Selenium DragnDrop not working properly

I am trying to moving a div with Selenium DragnDrop. Here is my Code
WebElement fromElement= driver.findElement(By.xpath("layoutDivPosition_locator"));
WebElement toElement=driver.findElement(By.xpath("layoutDivPosition_locator"));
Actions action = new Actions(driver);
Action dragnDrop = action.clickAndHold(fromElement).moveToElement(toElement).release(toElement).build();
dragnDrop.perform();
Please help for this .
Selenium's actions for drag and drop do not play nice with HTML5, when you trying to move element to some frame and in several other situations. Possibly you can solve your problem using this jQuery solution:
[C#][Selenium] How to drag-hover-drop an element