无法在C#中使用Selenium禁用硬件加速。

huangapple go评论76阅读模式
英文:

Cannot disable Hardware acceleration using Selenium in c#

问题

我正在使用C#中的Selenium库来打开和控制Chrome。当Chrome打开后(我正在测试回放事件),我会对显示器进行截屏并处理它们以确定某个事件发生的时间。问题在于,由于硬件加速(我认为这是问题的原因),截屏返回为空(黑色)。我如何强制Selenium代码中打开的Chrome禁用硬件加速?

我尝试过在Chrome本身中禁用硬件加速(在设置和chrome://flags中),但Selenium仍然以硬件加速打开Chrome。我尝试强制Selenium使用以下方式打开Chrome以禁用硬件加速:

new DriverManager().SetUpDriver(new ChromeConfig());
var options = new ChromeOptions();
options.AddArgument("--disable-gpu");
driver = new ChromeDriver(options);

但是代码似乎忽略了这个参数,仍然以启用加速的方式启动。

有人可以帮助我解决这个问题吗?我更希望不改变截屏方式,但如果有必要,我也愿意考虑。谢谢。

注意1: 我愿意接受非编程解决方案。如果有一种方法可以指导我的用户自行关闭硬件加速,我会考虑它是有效的(即使不是首选)解决方案。

注意2: 如果您认为这有助于解决问题,我甚至愿意切换到其他浏览器。

更新: 我尝试了类似的解决方案用于Firefox和Edge,但似乎也不能解决这个问题(至少在我通过Selenium启动浏览器后检查设置时,硬件加速仍然显示为已启用)。我还尝试在user-data / Preferences / Settings中添加 "hardware_acceleration": false,但结果似乎是相同的。

英文:

I am using Selenium library in c# to open and control Chrome. When the chrome is opened (I am testing playback events), I take screenshots of the monitor and process them to determine when certain event occurred. The problem is that due to hardware-acceleration (i suppose that is the problem), the printscreens come back empty (black). How can I force the chrome opened by Selenium in my code to have hardware-acceleration disabled?

I have tried disabling the hardware-acceleration in chrome itself (in settings and in chrome://flags), but Selenium opens chrome with HW-acceleration anyway. I have tried to force Selenium to open chrome with HW-acceleration disabled like this:

new DriverManager().SetUpDriver(new ChromeConfig());
                    var options = new ChromeOptions();
                    options.AddArgument("--disable-gpu");
                    driver = new ChromeDriver(options);

but the code just seems to ignore the argument and starts with acceleration enabled anyway.
Can anyone help me solve this problem? I would prefer not to change the way I take screenshots, but if that is necessary, that would be helpful as well. Thank you.

Note 1: I am open to solutions that are not programatical. If there is a way to instruct my users to switch of hardware acceleration themselves, I would consider it to be a valid (even if not preferred) solution.

Note 2: I am even willing to switch to a different browser, if you think that would help.

Update: I have tried similar solutions for firefox and edge and those does not seem to resolve the issue either (at least when I check settings after launching the browser via Selenium, hardware acceleration is shown as enabled).
I have also tried adding ""hardware_acceleration": false," to user-data / Preferences / Settings, but the result seems to be the same.

答案1

得分: 0

"Try this two arguments:

options.AddArgument("disable-gpu");
options.AddArgument("disable-software-rasterizer");"

英文:

Try this two arguments:

options.AddArgument("disable-gpu");
options.AddArgument("disable-software-rasterizer");

huangapple
  • 本文由 发表于 2023年5月22日 17:43:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定