Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无头Chrome和document.location.href更改_Javascript_Java_Selenium_Selenium Chromedriver - Fatal编程技术网

Javascript 无头Chrome和document.location.href更改

Javascript 无头Chrome和document.location.href更改,javascript,java,selenium,selenium-chromedriver,Javascript,Java,Selenium,Selenium Chromedriver,我正在尝试自动化页面上的搜索流程,这要求您登录以访问搜索表单。我正在使用chromedriver和java,配置如下: ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.setHeadless(true); chromeOptions.addArguments("--disable-gpu"); chromeOptions.addArguments("--disable-extens

我正在尝试自动化页面上的搜索流程,这要求您登录以访问搜索表单。我正在使用chromedriver和java,配置如下:

    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.setHeadless(true);

    chromeOptions.addArguments("--disable-gpu");
    chromeOptions.addArguments("--disable-extensions");
    chromeOptions.setExperimentalOption("useAutomationExtension", false);
    chromeOptions.addArguments("--enable-javascript");
    chromeOptions.addArguments("--no-sandbox","--disable-dev-shm-usage'");
登录后,我可以通过在相关文本框中输入适当的数据进行搜索。搜索结果页面有一个返回搜索查询页面的按钮,相关按钮和js函数如下所示:

<input class="button" type="button" name="NAME" value="Search Again" onClick="somefunc('some_hash_params_etc')">
.....
function somefunc(url){document.location.href=url; }

.....
函数somefunc(url){document.location.href=url;}
但在headless模式下,点击这个按钮会进入主页,再次询问我的登录凭证

对于相同的流,无头测试工作得非常好,每次单击按钮都会将我重定向到搜索表单,而不会强制我重新登录

我尝试将url手动设置为发送到js函数的值,并尝试在代码中执行js,但对于headless模式,结果是相同的,重定向回主页而不是搜索表单


当初始化webdriver或headless模式需要代码中的其他逻辑时,我是否应该添加其他选项

对于headless browser,您必须将窗口大小设置为事件触发。因为headless browser在没有窗口大小的情况下无法识别单击位置。请告诉我它是否有效

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("headless");
chromeOptions.addArguments("window-size=1920,1080");

您是否添加了等待期?您发送的配置也不起作用,但我明白您的意思。。我在搜索时更改了每页的结果配置,一旦返回的行没有溢出页面大小,事件就会被正确触发和处理(我已将结果数量从50个减少到7个)