I would like to know the on-screen position of some element. I know how to get the position of an element in Python Selenium webriver but how to get an offset from the left-top corner of the screen?
We can use following two commands to verify the presence of an element: verifyElementPresent – returns TRUE if the specified element was FOUND in the page; FALSE if otherwise. verifyElementNotPresent – returns TRUE if the specified element was NOT FOUND anywhere in the page; FALSE if it is present.
New Selenium IDE To get the unique coordinates of an element we shall create an object of the class Point which shall store the location of the webelement obtained from the getLocation method. Then the individual x and y coordinate values can be computed from the getX and the getY methods respectively.
To get the width and height of a rendered web element programmatically using Selenium in Java, use WebElement. getSize() function. We first find the web element by name, id, class name, etc., and then call the getSize() function on this web element. getSize() function returns a Dimension object.
window.screen.top - Returns the distance in pixels from the top side of the current screen. windows.screen.left - Returns the distance in pixels from the left side of the main screen to the left side of the current screen. Unfortunately, I'm looking for something more browser independent.
How do you check scroll position using selenium? We can check scroll position using Selenium. To check the position we shall use the Javascript executor. We have to verify the value of the window.pageYOffset in the browser. While the URL is launched, the scroll is at the top the value of window.pageYOffset is 0.
outerHeight - innerHeight + screenY gives the offset relative to physical screen coordinates in Chrome. Very helpful. I've been looking around for the same thing, and surprisingly, this is tricky to implement.
I guess it's not possible to define distance from top-left corner of browser window to top-level corner of screen with just selenium
. But you can try to implement following:
driver = webdriver.Chrome()
driver.maximize_window() # now screen top-left corner == browser top-left corner
driver.get("http://stackoverflow.com/questions")
question = driver.find_element_by_link_text("Questions")
y_relative_coord = question.location['y']
browser_navigation_panel_height = driver.execute_script('return window.outerHeight - window.innerHeight;')
y_absolute_coord = y_relative_coord + browser_navigation_panel_height
x_absolute_coord = question.location['x']
There is no way to do this with 100% accuracy, but here is the best workaround that considers the browser window's offset, the toolbars in the window, and the scrolling position of the document:
# Assume there is equal amount of browser chrome on the left and right sides of the screen.
canvas_x_offset = driver.execute_script("return window.screenX + (window.outerWidth - window.innerWidth) / 2 - window.scrollX;")
# Assume all the browser chrome is on the top of the screen and none on the bottom.
canvas_y_offset = driver.execute_script("return window.screenY + (window.outerHeight - window.innerHeight) - window.scrollY;")
# Get the element center.
element_location = (element.rect["x"] + canvas_x_offset + element.rect["width"] / 2,
element.rect["y"] + canvas_y_offset + element.rect["height"] / 2)
page_offset = self.driver.execute_script("return window.pageYOffset;")
ih = self.driver.execute_script("return window.innerHeight;")
oh = self.driver.execute_script("return window.outerHeight;")
chrome_offset = window_size[1]-ih
ex = pos["x"]+8+element_size["width"]/2
ey = (pos["y"]-page_offset)+chrome_offset-(element_size["height"]/2)
pyautogui.moveTo(ex,ey,2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With