selenium scroll element into (center of) view
This should work in order to scroll element into center of view:
WebElement element = driver.findElement(By.xxx("xxxx"));
String scrollElementIntoMiddle = "var viewPortHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);"
+ "var elementTop = arguments[0].getBoundingClientRect().top;"
+ "window.scrollBy(0, elementTop-(viewPortHeight/2));";
((JavascriptExecutor) driver).executeScript(scrollElementIntoMiddle, element);
Way to scroll element in the middle of the screen
Below is the way JavaScript allowed me to resolve the mentioned problem.
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView({block: 'center'});", PassIWebElementOverHere);
Scroll Element into View with Selenium
Selenium 2 tries to scroll to the element and then click on it. This is because Selenium 2 will not interact with an element unless it thinks that it is visible.
Scrolling to the element happens implicitly so you just need to find the item and then work with it.
Scrolling to element using webdriver?
You are trying to run Java code with Python. In Python/Selenium, the org.openqa.selenium.interactions.Actions
are reflected in ActionChains
class:
from selenium.webdriver.common.action_chains import ActionChains
element = driver.find_element_by_id("my-id")
actions = ActionChains(driver)
actions.move_to_element(element).perform()
Or, you can also "scroll into view" via scrollIntoView()
:
driver.execute_script("arguments[0].scrollIntoView();", element)
If you are interested in the differences:
- scrollIntoView vs moveToElement
Scroll Element into View with Selenium
Selenium 2 tries to scroll to the element and then click on it. This is because Selenium 2 will not interact with an element unless it thinks that it is visible.
Scrolling to the element happens implicitly so you just need to find the item and then work with it.
Selenium Scroll into view doesn't work when element is already into view
You can check if element is in view port or not and based on the boolean, you can put the action, to find out if element is in the view port I use
public static Boolean isVisibleInViewport(WebElement element) {
WebDriver driver = ((RemoteWebElement)element).getWrappedDriver();
return (Boolean)((JavascriptExecutor)driver).executeScript(
"var elem = arguments[0], " +
" box = elem.getBoundingClientRect(), " +
" cx = box.left + box.width / 2, " +
" cy = box.top + box.height / 2, " +
" e = document.elementFromPoint(cx, cy); " +
"for (; e; e = e.parentElement) { " +
" if (e === elem) " +
" return true; " +
"} " +
"return false; "
, element);
}
Reference
Selenium webdriver with Python on chrome - Scroll to the exact middle of an element
Actually selenium itself try to click on element at center position of element, so this exception normally occurs when target element overlayed by other element due to size of the window or any other reason, like it would be hidden inside scroll bar etc.
So basically if you want to get exact element into view port, so you could click on it, you should try using scrollIntoView()
method which scrolls the current element into the visible area of the browser window as below :-
element = driver.find_element..
driver.execute_script("arguments[0].scrollIntoView()", element)
Related Topics
Nested Forms in Rails - Accessing Attribute in Has_Many Relation
Ruby Gets/Puts Only for Strings
Ruby Minitest: Suite- or Class- Level Setup
Show Full Path Name of the Ruby File When It Get Loaded
Travis Reports Odd Message of Corrupted Gemfile.Lock
New Rails Project: 'Bundle Install' Can't Install Rails in Gemfile
Error Installing Ruby with Rvm (Osx 10.8)
How to Pass Arguments to Define_Method
Trouble Comparing Time with Rspec
The Compiler Failed to Generate an Executable File. (Runtimeerror)
Ruby Block to String Instead of Executing
How to Fire Raw Mongodb Queries Directly in Ruby
What's the Point of Freezing Your Rails Version/Gems
Decrypting Salted Aes File Generated on Command Line with Ruby
Selenium-Webdriver Ruby --> How to Wait for Images to Be Fully Loaded After Click