简体   繁体   English

如何在 iframe 内的 cursor position 处获取元素?

[英]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.我正在尝试使用以下代码来获取位于光标 position 的元素,但该元素位于 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.我认为下面的代码应该在光标的 position 处获得最深的子元素,但它似乎没有做任何事情。 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()

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();在 C# 它是RemoteWebDriver.SwitchTo().Frame(); sorry, I don't know what it looks like in Python, you'll need to dig into the docs.抱歉,我不知道它在 Python 中是什么样子,您需要深入研究文档。

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();

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. 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.这使得通过 css 选择器的 select 元素更容易,我建议远离鼠标协调,因为比例因设备而异。 . . Sincerely hope this helped.真诚地希望这会有所帮助。 . . PS Try to use css selectors rather than xpath where you can as it is normally more versatile (less buggy). PS 尝试使用 css 选择器,而不是 xpath,因为它通常更通用(错误更少)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM