Friday, 28 May 2021

How to get element at cursor position within iframe?

I'm trying to use the below code to get the element located at the cursor's position, but the element is within an iframe. I thought the below code was supposed to get the deepest child element at the cursor's position, but it doesn't seem to do anything. What am I doing wrong?

When the page loads, I'm trying to click the "add one of everything to my cart" button.

from selenium import webdriver
from tkinter import *
from pynput import mouse
from pynput.mouse import Listener

def CheckRightClick(x, y, button, pressed):
    if button == mouse.Button.right:
        if pressed:
            click_window.event_generate("<<quit>>")
            print('Getting element')
            element_at_cursor_script = 'return document.elementFromPoint(' + str(x) + ',' + str(y) + ');'
            element = driver.execute_script(element_at_cursor_script)
            print('Element at cursor is ', element)

options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=options, executable_path=r'C:\Users\David\Desktop\Python\chromedriver_win32'
                                                           r'\chromedriver.exe')
url = 'http://store.cardsagainsthumanity.com/'
driver.get(url)

click_window = Tk()
click_prompt = Label(click_window, text='Right click somewhere')
click_prompt.grid(row=0, column=0)
click_window.bind("<<quit>>", lambda *args: click_window.destroy())
listener = Listener(on_click=CheckRightClick)
listener.start()
click_prompt.mainloop()
listener.stop()


from How to get element at cursor position within iframe?

No comments:

Post a Comment