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.
Related
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
How can I get the geb webdriver to move to an element on the page that may not be in the view of the browser. The element exists on the page and is displayed but there may be a possibility that the element will appear on the bottom of the page and the browser would need to scroll down in order to bring the element into view.
import geb.Page
class myPage extends Page {
static url = "mypage.xhtml"
static at = {title == "myPage"}
static content = {
someElement = {$("#bottomOfPage")}
anotherElement = {$(".someClass",5)}
}
void clickElement(){
//possibility to fail because element may not be displayed in browsers view
//if not in view, scroll to element, then click
someElement.click()
}
}
Using this page as an example, If an element is not in view on the page and possibly at the very bottom, how can I tell the webdriver to bring the element into view? I have some complicated tests that are failing when the page contents are not in view. In some cases, the driver will move to the element even if it's not in view. But I would like to explicitly tell the driver to move to an element if its not in view for the cases that it does not move on its own.
The selenium library does have a moveToElement() function but it only accepts objects of the WebElement class. Using geb, when creating page classes, all elements that are used in my tests are declared in the content section and thus are of the SimplePageContent class. SimplePageContent cannot be casted to a WebElement either. moveToElement(By.id("bottomOfPage")) does work but is not ideal because I do not want hard coded values. Id much rather pass the page content object itself if possible.
From my experience I can say that if you ask WebDriver to click on an element that is not currently in view it will first scroll to that element and then click on it. So you're either hitting some kind of bug if it does not happen or the fact that the element is not in view is not what's causing your element not to be clicked.
If you still want to move to the element explicitly using Actions.moveToElement() then you can easily turn objects returned from your content definition using the fact that they implement Navigator which comes with a firstElement() which you should use if you want to get the first WebElement of a multi element Navigator or singleElement() when your Navigator has exactly one WebElement.
There is also a simpler way, which won't require extracting WebElements from your Navigators - use an interact {} block:
interact {
moveToElement(someElement)
}
I'm running into an issue where I'm attempting to click on a checkbox. The app is written in AngularJS.
The checkbox can not be clicked, as technically the element is not visible. Using 'visible: false' does not help to get this box checked. I have also tried to use element.set(true)
The checkbox is within a label element that happens to also contain hyperlinks. Since Capybara/Selenium clicks on the middle of an element by default, I end up opening the hyperlink instead of marking the checkbox. Clicking within anywhere in the label (outside of the hyperlinks) checks the box successfully.
How can I achieve this?
I would recommend using JavascriptExecutor to click element. In case element is technically not visible Webdriver will not allow you to click it using API, as it tries to simulate real user actions. Nevertheless you have the option to execute JS directly, which doesn't really care.
This answer will help you with the code part:
https://stackoverflow.com/a/19934852/2998104
You would need to change ending slightly to:
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.xpath("exact_locator_of_checkbox"));
js.executeScript("arguments[0].click()",element);
Of course no need to use only xpath, but anything that help you point to the checkbox directly.
Took me a little while but I figured out that I can avoid the Capybara API and use WebDriver's ActionBuilder.
def agree
source = find('label.terms-label').native
actionbuilder = page.driver.browser.action
actionbuilder.move_to(source, 0, 0).click.perform
end
By default, the Capybara API (as well as other native Selenium methods) will click in the center of the element. This will avoid that and will find the element and click it at (0, 0) coordinates.
As of Capybara 3.0.0, you can specify an offset when you call click on an element. So if you want to click the top-left corner of the element box, you'd say:
find('label.terms-label').click(x: 0, y: 0)
(Note, you must specify :x and :y if you specify either)
API Docs
Relevant commit
On the screenhsot below is HTML-code of iframe.
The first two red marked object can be identified by Webdriver however the last marked (which is a button) can't be clicked by Webdriver. I have tried click it using different ways (like click by id, name, etc). But I still can't click the submit button.
Please help me to click that submit button inside the frame.
You need to use switchTo().frame() to access content within a frame or iframe.
driver.switchTo().frame("name"); // where name is the name of the iframe i.e. name="frameName", you would use framename
driver.switchTo().frame(0); // You can switch to the frame by index
driver.switchTo().frame(element); // You can switch to the frame by a WebElement reference
In your particular case you can use:
driver.switchTo().frame("InstantSgn");
To switch out of the frame after you're done within the iframe context:
driver.switchTo().defaultContent();
Same issue happened to me today. It might be simply due to you not switching to the frame, or it could be something I experienced.
This happened to me in IE only. In Chrome there is no problem at all.
Actual Code
As you can see, I did switch to the frame to make sure this element could be found. But, it could not be. The only solution that I found was to put Thread.Sleep(2000) to make it pass. I am not quite sure why, but I guess it has something to do with the content not being available in the DOM.
This question already has answers here:
Selenium webdriver can't click on a link outside the page
(11 answers)
Closed 9 years ago.
I want to click on element from a list but the problem is that, the element is present in list but due to huge size of that list required element is not visible on page (Current frame). To click on required element I have to scroll down the list then that element get visible.
please suggest me how to do that. I am using Selenium Webdriver using Ruby.
You can try this,capture element's xpath (or By id whatever you think is right) when it is visible and click on it using js
Here I want to click Sign Out which come visible in a drop down menu only on mouse hover.
WebElement hiddentElement = driver.findElement(By
.xpath("//*[#id='navright']/li[3]/ul/li[2]/a"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();",
hiddentElement);
Selenium is supposed to imitate the user behaviour, clicking on invisible elements isn't supported by default for that reason.
You will probably end up with an exception for doing that.
You can use the method getLocationOnScreenOnceScrolledIntoView to make WebDriver scroll the element into view:
if (element instanceof Locatable) {
Locatable remoteElement = (Locatable) inputElement;
remoteElement.getLocationOnScreenOnceScrolledIntoView();
}
Used the below solution once in a similar situation and it worked for me. Basically get all the elements of the list and using Actions iterate through each one and get the text and if it is the required option then click on it using Actions class again.