6

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()
  • 1
    Hi! Does [this](https://stackoverflow.com/q/30039672/13552470) help? – Red May 26 '21 at 12:44
  • 1
    I've seen that post and tried using that script instead because it allowed a clearer way to use variables, but the program hangs and never reaches the print statement directly after, even when debugging line by line. –  May 26 '21 at 18:00
  • 1
    What do you mean by cursor's position ? – cruisepandey May 30 '21 at 08:01
  • 1
    The mouse cursor? –  May 30 '21 at 18:00

3 Answers3

1

You must switch the driver context to the frame in order to interact with elements in that frame. In C# it's RemoteWebDriver.SwitchTo().Frame(); sorry, I don't know what it looks like in Python, you'll need to dig into the docs.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
Conrad Albrecht
  • 1,976
  • 2
  • 15
  • 19
  • 1
    Thanks but I know I need to switch to the iframe, but I'm having trouble figuring out how to tell which iframe the element is in. I've been trying to iterate through every iframe and see if the correct element is in it, but since I can't actually grab the element I'm looking for, I can't figure out how to find the right one during the program's runtime. –  May 25 '21 at 22:16
1

See if this works:


driver.SwitchTo().Frame(driver.FindElement(By.xpath(".//iframe[@title='Cart']")));
driver.FindElement(By.xpath(".//a[text()='Add one of everything to my cart.']")).click();

itronic1990
  • 1,231
  • 2
  • 4
  • 18
  • 1
    I mentioned in my post that I am looking for a solution that would work on any website, I'm only using the link in my code for testing purposes. I'm trying to locate the needed iframe at runtime without having to predefine it. –  May 26 '21 at 17:55
0

You can use the driver to go directly to the iframe url, just copy it from the iframe that comes up and you can access it directly rather than through a pop-up box.

driver.get('iframe.url')
driver.find_element_by_css_selector('iframeselector').click()

rather than

driver.get('website.url')
driver.find_element_by_css_selector('selector').click()
#click button that opens iframe
driver.find_element_by_css_selector('iframeselector').click()

This makes elements a lot easier to select via css selectors and I would recommend staying away from mouse coordination as scales vary from device-to-device. . Sincerely hope this helped. . P.S Try to use css selectors rather than xpath where you can as it is normally more versatile (less buggy).

Xpired
  • 35
  • 4