MCPcopy Create free account
hub / github.com/seleniumbase/SeleniumBase / get_gui_element_rect

Method get_gui_element_rect

seleniumbase/fixtures/base_case.py:3541–3574  ·  view source on GitHub ↗

Very similar to element.rect, but the x, y coordinates are relative to the entire screen, rather than the browser window. This is specifically for PyAutoGUI actions on the full screen. (Note: There may be complications if iframes are involved.)

(self, selector, by="css selector")

Source from the content-addressed store, hash-verified

3539 )
3540
3541 def get_gui_element_rect(self, selector, by="css selector"):
3542 """Very similar to element.rect, but the x, y coordinates are
3543 relative to the entire screen, rather than the browser window.
3544 This is specifically for PyAutoGUI actions on the full screen.
3545 (Note: There may be complications if iframes are involved.)"""
3546 if self.__is_cdp_swap_needed():
3547 return self.cdp.get_gui_element_rect(selector)
3548 element = self.wait_for_element_present(selector, by=by, timeout=1)
3549 element_rect = element.rect
3550 e_width = element_rect["width"]
3551 e_height = element_rect["height"]
3552 i_x = 0
3553 i_y = 0
3554 iframe_switch = False
3555 if self.__is_in_frame():
3556 self.switch_to_parent_frame()
3557 if self.__is_in_frame():
3558 raise Exception("Nested iframes breaks get_gui_element_rect!")
3559 iframe_switch = True
3560 iframe = self.wait_for_element_present("iframe", timeout=1)
3561 i_x = iframe.rect["x"]
3562 i_y = iframe.rect["y"]
3563 window_rect = self.get_window_rect()
3564 w_bottom_y = window_rect["y"] + window_rect["height"]
3565 viewport_height = self.execute_script("return window.innerHeight;")
3566 x = math.ceil(window_rect["x"] + i_x + element_rect["x"])
3567 y = math.ceil(w_bottom_y - viewport_height + i_y + element_rect["y"])
3568 y_scroll_offset = self.execute_script("return window.pageYOffset;")
3569 y = int(y - y_scroll_offset)
3570 if iframe_switch:
3571 self.switch_to_frame()
3572 if not self.is_element_present(selector, by=by):
3573 self.switch_to_parent_frame()
3574 return ({"height": e_height, "width": e_width, "x": x, "y": y})
3575
3576 def get_gui_element_center(self, selector, by="css selector"):
3577 """Returns the x, y coordinates of the element's center based

Callers 1

Calls 8

__is_cdp_swap_neededMethod · 0.95
__is_in_frameMethod · 0.95
get_window_rectMethod · 0.95
execute_scriptMethod · 0.95
switch_to_frameMethod · 0.95
is_element_presentMethod · 0.95

Tested by

no test coverage detected