0

Python-selenium documentation has examples of locating elements based on id, class, name, xpath, etc.

How would I go about locating elements based on the click location from the mouse?

3
  • Can you give an example scenario?
    – Alexey R.
    Commented Feb 5, 2020 at 22:14
  • What are you trying to accomplish? The x/y coordinates? Because you normally don't locate elements based on a mouse click location.
    – Lee Jensen
    Commented Feb 5, 2020 at 23:30
  • Is this useful: stackoverflow.com/questions/6775351/…?
    – Mate Mrše
    Commented Apr 6, 2020 at 12:38

2 Answers 2

1

I think this is what you are trying to do, the below command gets the currently active element (In your case , you will get element where the mouse is blinking )

elem = driver.switch_to.active_element

and you could also get the web element tag as

val = elem.get_attribute("outerHTML")

Print the val and you can see the html of the element identified by the click

print(val)  #this gives the full html of that particular element eg <div id="answer-42436" class="answer" data-answerid="42436" itemprop="suggestedAnswer" itemscope="" itemtype="http://schema.org/Answer">
1
  • 1
    Please put some comments when downvoting, help others know what is the better approach than this.
    – PDHide
    Commented Feb 6, 2020 at 0:11
0

If you are trying to get the absolute x and y values then try this code.

# 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")

# get to 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 the absolute x value    
abs_x = askQuestion.location['x']

# get y value    
y = askQuestion.location['y']
abs_y = y + panel_height

# print x and y values
print ("Absolute x : " + abs_x)
print ("Absolute y : " + abs_y)

Also, you can try this using this

1
  • Hello, @user43474 have you tried?
    – Meet
    Commented Feb 7, 2020 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.