使用基于 Chromium 的浏览器使用 Selenium 更改 window 大小

Changing window size with Selenium used with a Chromium based browser

我正在使用 Selenium 2.35.0 来测试基于 Chromium 的浏览器。我正在使用 Chrome 驱动程序版本 2.43.600233,并将我的应用程序名称设置为 Chrome,将版本设置为 2.43.600233,如
https://forum.qt.io/topic/96202/unrecognized-chrome-version-when-using-selenium-python-bindings-and-chromedriver

"I had the same problem, and a way to hack it is to set your QT applicationName and applicationVersion to "Chrome" and "69.0.3497.128". "

然后一切正常。当我尝试使用

最大化、最小化、更改 window 的 window 大小时,我只是 运行 出了问题
driver.manage().window().maximize(); 
driver.manage().window().setSize(d);

我运行这个错误

{"code":-32601,"message":"'Browser.getWindowForTarget' 未找到"}。

当我搜索那个错误时,我发现了这个问题

答案提到

I warn you in advance that you should not use the window_size parameter, since Chromium does not support it, otherwise we will encounter an error: [Facebook\WebDriver\Exception\UnknownServerException] unknown error: unhandled inspector error: {"code":-32601,"message":"'Browser.getWindowForTarget' wasn't found"}.

有什么破解方法可以解决这个问题。当我 window 调整大小时,我需要测试我的浏览器。

非常感谢。

我可以告诉你,在所有最近的 Chrome 版本(几年前)中,使用最新的 Chrome 驱动程序,在 C# 中,设置 driver.Manage()。 Window.Size 对我来说很好用。但是,我不熟悉像您所做的那样设置应用程序名称和版本。如果不先调用 maximize() 是否有效?

我使用 xdotool http://manpages.ubuntu.com/manpages/trusty/man1/xdotool.1.html 调整了大小。 在我的 java 代码中我有这个函数

public void xdoWindowResize(String width,String height)
{
    String  title= driver.getTitle();
    try{
    ProcessBuilder pb = new ProcessBuilder("./resize.sh", title,width,height);
    Process p = pb.start();
    }
    catch (Exception e) {System.out.println(e);}
}

和resize.sh

#!/bin/sh
my_id=$(xdotool search --name "")
xdotool windowsize $my_id