Home > Net >  Python Selenium, click at a specified x y position
Python Selenium, click at a specified x y position

Time:12-24

So, I have looked at some very old questions but none of them answer my question, i want to click an absolute x y position but no DOMs no XPATHs nothing. Just x and y positions that need to be clicked. Thank you in advanced!

I already tried this and a lot more, but that linked above is the closest to what I want.

CodePudding user response:

According to the Selenium documentation.
You can click anywhere within the viewport using ActionBuilder

In the following code,

  • A sample webpage will open in Chrome
  • The page will be displayed for 5 seconds.
  • The mouse pointer will move to location (64,60) which happens to be a link on the page.
  • The mouse will click at that location, thus clicking on the link.

Note:

You will not see your mouse icon move to the location on the screen, however, the mouse pointer does actually move to that position.

Code:

from time import sleep
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder


# Open Webpage
driver = webdriver.Chrome()
driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')

# Sleep for 5 seconds before clicking on the link at position (64,60)
sleep(5) 

# Move to position (64,60) and click() at that position (Note: you will not see your mouse move)
action = ActionBuilder(driver)
action.pointer_action.move_to_location(64, 60)
action.pointer_action.click()
action.perform()
  • Related