问题
I cannot locate elements using headless mode because of this restriction "All users will have to use google Chrome when accessing our sites." This restriction was added by our admins so users could use only Google chrome.
My code is
@Test(priority = 1)
public void setupApplication() throws IOException {
/*
* open browser (GoogleChrome) and enter user credentials
*/
ChromeOptions options = new ChromeOptions();
options.addArguments("--window-size=1920,1080");
options.addArguments("--disable-gpu");
options.addArguments("--disable-extensions");
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("--proxy-server='direct://'");
options.addArguments("--proxy-bypass-list=*");
options.addArguments("--start-maximized");
options.addArguments("--headless");
driver = new ChromeDriver(options);
driver.get("link");
log.info("Launching chrome browser");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("C:/Automation Testing/scr3.png"));
}
Unfortunately I cannot show our link. My question is how to bypass this and find elements?
Thanks in advance! enter image description here
回答1:
Update
if you wish to bypass the headless agent footprint attach following argument:
--user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.50 Safari/537.36
Note: any version you apply to the user-agent argument will be displayed in the request header information.
...or speak with the 'admins' of your project so they can include the headless chrome agent to the white-list.
Here is a normal agent information from chrome:
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
Here is the headless chrome
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/69.0.3497.100 Safari/537.36
As you can see the headless chrome agent is called: HeadlessChrome
来源:https://stackoverflow.com/questions/52589782/cannot-locate-elements-using-headless-mode-selenium