Locating element's screen position in Selenium (using Python) - python

Problem similar to the one described here.
I want to get the mouse from point a to point b along a curve that I define. So, I need a and b's location coordinates. Point a is the mouse's current location, and point b is an element that I can locate. I can find the element's position using the following:
element = driver.find_element_by_class_name("insertnamehere")
x, y = element.location["x"], element.location["y"]
Unfortunately, I can't use similar Selenium methods to find the mouse's current location (as far as I know). So I have to find the mouse like this:
import pyautogui
x_mouse, y_mouse = pyautogui.position()
Pyautogui gives screen location, which--as the above linked question described--is not what element.location gives. But the linked solution doesn't work for me on the site that I'm interested in. That is, I don't get matching coordinates. The site is this one.
I suspect it's because Selenium's locator doesn't count the map element as part of the web page, and returns coordinates relative to the listings (the part that you can scroll). So, here are some ideas I've tried:
Convert the screen location into Selenium location, or vice versa.
This is difficult because I think the formula (screen to Selenium) will depend on my monitor, so won't work generally.
Put the mouse on a web element so that point a can be located using Selenium's .location.
Might be the best solution, but I would have to move to the element using ActionChains(driver).move_to_element(), which defeats the purpose of defining an a -> b mouse path.
Either find the mouse location using Selenium's location method or find the element in terms of screen coordinates.
I've tried pyautogui's locateOnScreen(). Seems promising, and I will probably just do it this way.

If you are trying to get the absolute x and y values (x and y values relative to screen) then use the below logic.
# maximize the browser window so that you can get the absolute x and y based on the window inner and outer heights
driver.maximize_window()
# navigate to OP
driver.get("https://stackoverflow.com/questions/56688961/locating-elements-screen-position-in-selenium-using-python")
# get ask question element
askQuestion = driver.find_element_by_link_text("Ask Question")
# get the browser panel height
panel_height = driver.execute_script('return window.outerHeight - window.innerHeight;')
# get absolute x value
abs_x = askQuestion.location['x']
# get y value (this is relative y value - meaning y value with in browser)
y = askQuestion.location['y']
abs_y = y + panel_height
# print the absolute x and y values
print ("Absolute x : " + abs_x)
print ("Absolute y : " + abs_y)

Related

Using selenium in Python, I need to search the page for a given string, then click the dynamic button element next to the string

Imagine I am using a website interface, similar to an online survey. My code can navigate and complete the form by clicking the necessary button. I need to be able to identify which question to click based on a given input.
key_str = '10.'
element = driver.find_element(By.ID, key_str)
We now have to move a few elements forward to click the box that will exist to the right of it. I cannot simply direct xpath to the button, as I will not know where it will be prior to opening the webpage. This needs to be a 1 size fits all type of solution. That said, we will always find "Question #10", and there will always be a button nearby the question.
Ideally the code will be able to identify the position that the "key_str" element resides, feed back this positioning as some sort of variable, then use that variable to find 'button' element that is to the right of it on the page.
Visually: (excuse the fake code here, more meant to be conceptual)
position = element.location() #figure out location of our key_str
if element contains key_str: #check we are in right place
button_pos = position + 3 #if in right place, move to button element
action = ActionChains(driver)
# click the button
action.click(on_element = button_pos)
All advice is appreciated!

Clicking in selenium python after using move_by_offset()

I am trying to navigate through a miniature window containing a grid (1000+ rows) which lies inside of a website. And my goal requires me to click each row of the grid. Initially I tried locating each row using xPATH but there is no reliable way to do this because when I scroll down the grid, the xPATH changes (eg. row 1 -> ...div/div[1]/..., row n -> ...div/div[6]/...)
therefore to get around this issue, I used actionChains move_by_offset and located the row I want this way. and using a loop to add on some grid-width each time, before hitting some maximum number of rows that can appear on screen and scrolling down using a.send_keys(Keys.PAGE_DOWN).
This is all working well and i can see the hover animation above the rows when i want them. However, I cannot get the actionChains to click on the rows when hovering. I have tried click, double click and click_and_hold + release to no success.
Any help on either making move_by_offset + click work or using xPATH's more effectively would be great.
For context, I am using this on website for work so I cannot release the URL/photos of it. This is my first time using selenium.
for s in range(n):
scrollLim = 7
base = 180
for x in range(scrollLim):
print(str(x))
a.reset_actions()
a.move_by_offset(60, base + 60 * x).double_click()
a.perform()
time.sleep(1)
if x == scrollLim - 1:
a.send_keys(Keys.PAGE_DOWN).perform()

How to find coordinates(x, y) of checkbox with Selenium python

I would like to find the position (x, y) of a checkbox, but unfortunately after several searches I have not found solutions ... (Even following the instructions in this answer: Selenium: get coordinates or dimensions of element with Python)
Here is my code:
E = wait (browser, 20) .until (EC.element_to_be_clickable ((By.ID, 'recaptcha-anchor')))
Location = E.location
Print Location
Unfortunately, this action returns me coordinates not going towards the direction of the checkbox in question ...
Did you have a solution, or is it really impossible to do this?
Thank you in advance!

Capture the value of an element on a web page using the coordinates of the element

I have the coordinates (x, y) of an element on a web page. How do I capture the value stored in that element at that position. More specifically I want to capture the value that is shown on the web page at that particular location.
Is there a way to do this with the Selenium Python bindings?
Execute the javascript code and get the element using elementFromPoint():
x = 100
y = 100
element = driver.execute_script('return document.elementFromPoint({x}, {y});'.format(x=x, y=y)

python selenium move_to_element_with_offset, passing in float offset

The following code, moves to the mid point of the element & performs the click
action_chains = ActionChains(driver)
action_chains.move_to_element_with_offset(element, 33.8333, 0).click().perform()
But the following code, actually clicks at 34px from the top right corner of the element(which is desired).
action_chains = ActionChains(driver)
action_chains.move_to_element_with_offset(element, 34, 0).click().perform()
But I want to actually click at 33.8333 px from the top-right corner, are there any issues if I pass in float values instead of integers ? Any workarounds or am I missing something?
I am not sure about the specific case you are facing, but I have heard of people struggling with similar issues. If I were you, I would look for a javascript solution in conjuntion with driver.execute_async_script

Categories

Resources