我正在尝试使用下面的代码来获取位于光标位置的元素,但该元素位于 iframe 内。我认为下面的代码应该在光标位置获取最深的子元素,但它似乎没有做任何事情。我究竟做错了什么?
当页面加载时,我试图单击“将所有内容之一添加到我的购物车”按钮。
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 …Run Code Online (Sandbox Code Playgroud) javascript python selenium selenium-chromedriver selenium-webdriver
我正在尝试在基于 Selenium 的框架中实现一个“对象选择器”,这在大多数商业自动化工具中都很常见。为此,我使用 Javascript 命令来查找鼠标位置处的元素,但我没有得到我期望的元素。
如果我使用 ChromeDriver 或 InternetExplorerDriver,脚本始终返回标头对象。无论我看什么网页或鼠标的位置。虽然听起来脚本采用的是坐标 0, 0 而不是鼠标位置,但我已确认 Cursor.Position 正在发送正确的值。
如果我使用 FirefoxDriver 我会得到一个异常:
"Argument 1 of Document.elementFromPoint is not a finite floating-point value. (UnexpectedJavaScriptError)"
Run Code Online (Sandbox Code Playgroud)
谁能看到我做错了什么吗?
private void OnHovering()
{
if (Control.ModifierKeys == System.Windows.Forms.Keys.Control)
{
IWebElement ele = null;
try
{
// Find the element at the mouse position
if (driver is IJavaScriptExecutor)
ele = (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript(
"return document.elementFromPoint(arguments[0], arguments[1])",
new int[] { Cursor.Position.X, Cursor.Position.Y });
// Select the element found
if (ele != null)
SelectElement(ele);
}
catch …Run Code Online (Sandbox Code Playgroud)