python中使用selenium显示鼠标光标的方法

问题描述 投票:0回答:3

我尝试在使用 ActionChains 时单击 chrome 浏览器。

from selenium.webdriver import ActionChains

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(URL)
link = driver.find_element_by_id('link')
action = ActionChains(driver)
action.double_click(advanced_options_link).perform()

但还有什么?!
我想在浏览器上看到鼠标光标以监视到底发生了什么。
*** 请帮忙 ***

python selenium webdriver
3个回答
2
投票

我过去需要使用这样的功能。据我研究,我了解到硒是不可能的。您可以将鼠标悬停在元素上,甚至将光标移动到屏幕上的特定位置,但您看不到它移动,因为它并没有真正移动光标,硒无法以这种方式控制光标。

你可以尝试研究一下,你也许可以使用selenium之外的东西来控制计算机的实际光标。但是,即使有可能,仍然很难让它可靠地工作,因为你无法像使用 Selenium 那样控制网站,所以一切都变成手动的,因此很难且热衷于生成错误。


1
投票

selenium
中没有内置选项来显示操作创建的光标。但是您可以使用下面的脚本在测试进行时在 DOM 内添加一个
div
,当单击时会显示一个点。

cursor_script = '''
var cursor = document.createElement('div');
cursor.style.position = 'absolute';
cursor.style.zIndex = '9999';
cursor.style.width = '10px';
cursor.style.height = '10px';
cursor.style.borderRadius = '50%';
cursor.style.backgroundColor = 'red';
cursor.style.pointerEvents = 'none';
document.body.appendChild(cursor);

document.addEventListener('mousemove', function(e) {
  cursor.style.left = e.pageX - 5 + 'px';
  cursor.style.top = e.pageY - 5 + 'px';
});
'''

driver = webdriver.Chrome()
driver.get('https://example.com')

driver.execute_script(cursor_script)

-1
投票

如果您尝试使用

move_to_element()
中的
ActionChains
是否有效?像这样的东西;

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
driver.get(URL)
link = driver.find_element_by_id('link')

hover = ActionChains(driver).move_to_element(link)
hover.perform()
© www.soinside.com 2019 - 2024. All rights reserved.