0

I am using selenium script to test login flow. After entering username, I need to click next button and it should redirect me to Identity provider page. If I don't run my selenium script in headless mode, it works fine, but same things fails when I run in headless mode. Here are the args that I am setting for chromeDriver

ChromeOptions options = new ChromeOptions(); options.addArguments("--headless=new"); // Run in headless mode options.addArguments("--disable-gpu"); // Applicable to Windows machines options.addArguments("--no-sandbox"); // Bypass OS security model options.addArguments("--window-size=1920,1080"); // Set window size options.addArguments("--disable-application-cache"); options.addArguments("--ignore-certificate-errors"); options.addArguments("--start-maximized"); options.addArguments("--enable-javascript"); driver = new ChromeDriver(options); 

Below is the code for button what I am trying to find and click

WebElement nextButton = driver.findElement(By.xpath("//*[@id='next-btn' or @title='Next']")); (JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", nextButton); ((JavascriptExecutor) driver).executeScript("arguments[0].click();", nextButton) // Dispatch the 'input' event to simulate user interaction ((JavascriptExecutor) driver).executeScript("var event = new Event('input', { bubbles: true }); arguments[0].dispatchEvent(event);", nextButton); System.out.println("Button clicked"); wait.until(ExpectedConditions.urlContains("https://myidppage.com")); 

Looks like click() is working as its printing 'Button clicked' but the next line where I am expecting a certain webpage to load isn't working. I debugged and ffound out it remains on same page. While same is working in non-headless mode.

Please help me understand whats going wong

1
  • On a side note you can probably just use nextButton.click() instead of executing scripts there. (you won't have to scroll into view or anything like that) This could possibly be caused by use of visibility API by the site: developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API You might just try using another .get() sometime after button is clicked.CommentedSep 27, 2024 at 20:53

1 Answer 1

0

Figured out. Request was getting blocked by my Organization's cloudfront interpreting it as a bot. Added line

options.addArguments("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");//Adding a custom user-agent string to the ChromeOptions to mimic a real browser 

and it started working.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.