Selenium - 如何找到画布上 X Y 位置的像素颜色?

3
我正在使用Python的Selenium,在canvas上有一个问题:我想获取x y位置的像素颜色。
Selenium有一些类似于颜色选择器的东西吗?
我尝试使用getCssValue获取像素的颜色,但它不起作用,因为它是在canvas上。
以下是我尝试实现的示例:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
chrome_options = Options()

prefs = {'profile.default_content_setting_values': {'images': 1,
                            'plugins': 2, 'geolocation': 2,
                            'notifications': 2, 'auto_select_certificate': 2, 'fullscreen': 2,
                            'mouselock': 2, 'mixed_script': 2, 'media_stream': 2,
                            'media_stream_mic': 2, 'media_stream_camera': 2, 'protocol_handlers': 2,
                            'ppapi_broker': 2, 'automatic_downloads': 2, 'midi_sysex': 2,
                            'push_messaging': 2, 'ssl_cert_decisions': 2, 'metro_switch_to_desktop': 2,
                            'protected_media_identifier': 2, 'app_banner': 2, 'site_engagement': 2,
                            'durable_storage': 2}}
chrome_options.add_experimental_option('prefs', prefs)
chrome_options.add_argument("disable-infobars")
chrome_options.add_argument("disable-gpu")

driver = webdriver.Chrome(options=chrome_options, executable_path=r"C:\gecko\chromedriver.exe")
driver.get("https://drawisland.com/")

act = ActionChains(driver)
canvas = driver.find_element_by_tag_name("canvas")
act.move_to_element_with_offset(canvas,100,100).GetColor().perform()

您的问题已经被点赞了。您找到解决方案了吗? - JarsOfJam-Scheduler
2个回答

1

显然,使用selenium直接实现这一点是不可能的。


0

你无法直接获取像素的颜色,因为你需要一个元素来请求颜色。

Python 背景颜色片段

background_colour = element.value_of_css_property('background-color'))

你可以创建一个元素或整个页面的截图,然后使用像ImageMagick这样的库来验证图像。自从Selenium 4.0以来,对于处理颜色有了有限的支持。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接