How to scroll till an element that is inside an i frame - java

I need to click some element that is inside an iframe. When page is loaded these elements are not visible and I scroll down to make them visible. I tried as follows:
Move to iframe and switchTo(frame)
javascript.executeScript(argument[0].scrollIntoView(true), webelement)
Scrolling is happening only once. When I click first element. I need to scroll down and click the next element (it is a submission form in ui). All elements are in iframe.
Can anyone suggest any idea I'am using java with selenium.

Try this:
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);", myIWebElement);

Related

Selenium Click method performs click slightly outside the element boundary

I have an application created in ReactJS which also contains some EXTJS/jsp iframes inside it. Now when I try to click some anchor tag element (Hyperlink) using selenium click method it wasn't working. After some investigations, I found that the click is performed slightly above the element boundary. The selenium is able to identify the element using xpath but is unable to click the link correctly. Also tried using the action but it didn't work. Using offset is not an option as it relies too much on the screens resolution.
Any help is highly appreciated.
maybe you can set the width and height of the page to become larger. Some elements overlaps if the page does not support smaller screens.
If your locator is inside the IFrame, you will need to do a switch to that IFrame and then perform the click action, and after you need to switch out from that IFrame:
driver.switchTo().frame("your_iframe_name");
driver.findElement(By.xpath("your_locator")).click();
After use one of these to switch back:
driver.switchTo().parentFrame();
driver.switchTo().defaultContent();
Also, there is another way to click on a element located inside an IFrame, by executing a js script.
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.xpath("your_locator"));

Selenium WebDriver Java cannot find element in Chrome to scroll to

I have a Web application with a scroll-able menu on the left hand side. My code is pretty simple:
WebElement elementToScrollTo = driver.findElement(By.xpath(locator));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", elementToScrollTo);
locator is the link to the web element/menu item I want to click. The problem is in Chrome, the first line throws a NoSuchElementException. So obviously I cannot scroll to something that cannot be found in the first place.
However, what's weird is that in IE everything works perfectly. The element can be found and the menu scrolls. After which I use the element (i.e. click on it).
I cannot scroll the entire browser window as I only need the menu panel on the left hand side to scroll.
I have the latest Chrome (60.0.3112.78 (Official Build) (64-bit)) and chromedriver 2.30.
Note: I've actually figured out the problem. The reason it cannot find it is because it doesn't scroll to the parent of the menu item, the level 2 element. so the second line (scrolling) doesn't do anything in Chrome and because of that the driver will not find the menu item on level 3 (the child of level 2). So maybe scrolling doesn't work in Chrome?
It might be problem with the HTML elements located within a frame. To deal with such elements you need switch the frames first.
Can you try this command:
driver.switchTo().frame()
Also, try to use implicitWait if the above one doesn't work. Maybe, your element is taking time to load and therefore web driver fails to locate it.
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

How can i locate web element for 'dropdown-toggle' window in selenium webdriver

I can't able to click 'Board' link for the 'dropdown-toggle' window using selenium webdriver.
I have tried
cssSelector="a.accordion-toggle" (or) linkText="Board".
But it is not working.
SO, kindly give me a solution for how can I locating 'Board' webelement.
Please refer the screenshot.
Thanks,
Vairamuthu.
I think the problem is visibility of that element. Maybe you're trying to click it when the dropdown 'Academic Year' is collapsed and link 'Board' is invisible in the browser.
I see two possible solutions:
Click 'Academic Year' (or scroll mouse on it if it works this way) before clicking 'Board'
Use click with JavaScript:
driver.executeScript("arguments[0].click();", element);
Where "driver" - RemoteWebDriver object, "element" - RemoteWebElement
Note that the second solution won't work if the element for 'Board' link is absent in the page source when dropdown is collapsed.

Selenium: Clicking on toaster issues

I'm trying to click on an element on a page; the element is clearly visible on screen. There is a toaster that might pop up, so I'm trying to write a defense: if the toaster is on screen, close the toaster first, then continue clicking through to the next page. I am using PageFactory, so I have an element to contain the toaster and one for the close button for the toaster. My "deal with toaster" method is as follows:
if (driver.findElements(By.cssSelector("#toaster")).size() > 0
&& toaster.isDisplayed()) {
toasterClose.click();
}
When I do this in chrome, however, I'm getting org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (994, 758)
Pausing the test execution, I cannot see the toaster on the screen. I figure the devs must be hiding it by making it render in a far away, unscrollable location. So as a stopgap measure, I added a condition that if the x coordinate was greater than 800, don't click. With that in place, I get:
org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (547, 725). Other element would receive the click: <div id="toaster">...</div>
What's going on? How can the toaster not be clickable but would somehow receive the click anyway? Firefox can handle the test just fine, with or without the 800 pixel workaround; it's only Chrome having this issue.
For Clarification: The goal of the test is NOT to click the toaster. The goal is to click another element on the page. The test reported that a toaster was in the way, so I attempted to write a step to close the toaster if it is displayed. I have not seen this toaster, so I'm not exactly sure what it is, but chrome keeps reporting that it's in the way. All our toasters site-wide use a basic template that includes a close button so the user can close the toaster, which is what I'm trying to click. Firefox never has this issue and does not report the existence of any toasters.
I'm calling it a toaster because that's what our site calls it, because that's what it's called in whatever framework we got it from (jQuery UI? Backbone?). If I pause execution, I cannot see any toasters at this point in the test, but jQuery tells me it exists and is visible. However, the element found with jQuery has just the default pieces of our toaster setup: a div, an empty div where the message should be, and the close button. Clearly it's not meant to be rendered at this time, but Chrome thinks it's in the way.
I'm assuming by "toaster" you mean some sort of javascript modal popup with a close button.
Identifying the correct problem
You're testing the existence and visibility of the #toaster element, but not the toasterClose element that you're clicking. There's no guarantee that just because one element exists and is displayed, another is as well. From the error, it appears that the #toaster element overlaps the toasterClose element, making it unclickable.
Troubleshooting clickability
Once you've properly selected toasterClose, manually use devtools and inspect to see why it's unclickable. Is it visible and unobstructed? Is the toasterClose element something of zero height/width? Is there dynamic JavaScript modifying the page post-load? Is it actually positioned in view of the page? (I've had elements render visibly at the edge of the window only to be obstructed by the browser's scroll bars.1)
Alternative
You should also see if you really need to use this toasterClose element. How does would a human close this popup? Would they press Escape? Would they click outside the popup window, on the overlay element? Do they do something else that triggers some sort of closeModal() javascript function? You can also do any of these things using Selenium.
Last Resort
One thing you can always do to remove such a popup is to run your own javascript to modify the DOM and remove the offending element(s) altogether:
driver.execute_script(<<-javascript)
var toaster = document.getElementById("toaster");
toaster.parentNode.removeChild(toaster);
var overlay = document.getElementById("modal_overlay");
overlay.parentNode.removeChild(overlay);
javascript
Future/Additional
If this is a regular issue for you, I would suggest wrapping this code in try/catches and a retry mechanism to make it resilient to javascript dynamically loaded elements.
1 Update
Just to elaborate on the scrollbar issue I had, because it turned out that it was a very similar problem to yours.
Here, the "I'm Feeling Lucky" button is out of view. If Selenium tries to click on it, first it will attempt to scroll it into view.
Here's an example of what Selenium would attempt to do. Notice how the button is now "in view".
However, Chrome on OSX is styled in such a way that the scrollbars are normally hidden. The moment that Selenium issues the scroll command, the scrollbars appear and the following click command fails to reach the button.
The solution was to use javascript to scroll the window manually:
page.execute_script(<<-javascript)
document.getElementById("gbqfsb").scrollIntoView(true);
// or if that doesn't work:
window.scrollTo(0, document.getElementById("gbqfsb").getBoundingClientRect().top);
javascript
Try the following code. Should work:
if (driver.findElements(By.cssSelector("#toaster")).size() > 0
&& toaster.isDisplayed()) {
Actions builder = new Actions(driver);
builder.moveToElement(toasterClose).moveByOffset(2,2).click().build().perform();
}
Can you close toaster using escape key from keyboard manually.
If you can than use following:
Actions action = new Actions(driver);
action.sendKeys(Keys.ESCAPE).build().perform();
It seems the toaster was partially rendered and fixed to the DOM just below the bottom edge of the screen, using position:fixed to stop it from showing up until it's ready to be populated with data and animated onto the screen. When chrome tried to click on links that were below the bottom edge of the screen, it predicted that it'd hit the toaster and didn't actually bother scrolling.
After some googling, I added the following utility function:
public static void ScrollElementIntoView(WebDriver driver, WebElement element) {
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
}
Then I call this method before clicking any link on that page, and voila, no more toaster problems!

Xpath is found even though web object not visible

I am automating a code using selenium 2.0. I select one (or several) user(s) from a list. Then I click on an add button which makes the user name(s) visible on a grid. Each user will have a valid Xpath when visible on the grid. However, even after erasing all user names from the grid which actually disappears if there is no user names displayed, the Xpath still does not return null. I am using Xpath to check if it returns null when the object (user name) is not visible, but it does not work as expected. Is there any other way to solve my problem? I am pretty new with Selenium. I am using selenium 2.0. Bellow is a section of my code. Your help will be very appreciated.
//Check if user is present on the grid
By checkuser = By.xpath( ".//*[#id='sharing_list']/tbody/tr/td[1]/span");
//if the grid is not empty, which means the grid is visible...
if(null!=checkuser) //where the problem is!!
{
//Click the button to erase the names in the grid, then the grid desapears
webDriver.findElement(By.xpath("//*[#id='sharing_list']/tbody/tr/td[4]/span")).click();
Thread.sleep(2000);
//more code
//............
}
I can see two things happening here:
First is that your XPath is generic enough that it is selecting some other element that isn't a user. To see if this is the case, then in Chrome, go to the page and do the necessary actions to get it in the state you want. Next, press Ctrl-Shift-J, click on Console, and type in $x("//*[#id='sharing_list']/tbody/tr/td[4]/span"). Chrome will then show you which element your selector is selecting.
Your task then, is to identify if its selecting some other element, or whether the element is just not visible. It is definitely possible to have an element on a page, but not visible, and WebDriver WILL select invisible elements (unless you are doing By.linkText()). If you want to check to see if an element is visible do a element.isDisplayed().

Categories

Resources