简体   繁体   中英

Headless Chrome and document.location.href change

I am trying to automate a search flow on a page, which requieres you to login to access the search form. I am using chromedriver with java with the configs below:

    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'");

After the login, I am able to make the search by entering proper data into the related textboxes. The search result page has a button to go back to the search query page, the related button and js function looks like below:

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

But in headless mode, clicking on this button ends up with the main page, where it asks for my login credentials again.

For the same flow, a non-headless test works perfectly fine, each button click redirects me to the search form without forcing me to relogin.

I have tried to set the url manually into the value sent to the js function and tried to execute js in the code, but result is the same for headless mode, redirected back to the main page instead of the search form.

are there any other options I am supposed to add while initializing the webdriver or headless mode requires some other logic in the code?

for headless browser you have to set the window size to fire on event.Because headless browser can't recognise where to click without window size.Let me know if it works.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM