无法使用无头模式Selenium定位元素
由于存在以下限制,我无法使用无头模式定位元素:所有用户在访问我们的网站时都必须使用google Chrome浏览器."此限制是由我们的管理员添加的,因此用户只能使用Google chrome.
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.
我的代码是
@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?
提前谢谢!在此处输入图片描述
更新
如果您希望绕过无头代理的足迹,请附加以下参数:
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,例如Gecko)Chrome/60.0.3112.50 Safari/537.36
--user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.50 Safari/537.36
注意:您应用于user-agent参数的任何版本都将显示在请求标头信息中.
...或与您项目的管理员"交谈,以便他们可以将无头的chrome代理添加到白名单中.
...or speak with the 'admins' of your project so they can include the headless chrome agent to the white-list.
以下是来自chrome的常规代理信息:
Here is a normal agent information from chrome:
用户代理:Mozilla/5.0(Windows NT 10.0; Win64; x64)AppleWebKit/537.36(KHTML,例如Gecko)Chrome/69.0.3497.100Safari/537.36
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
这是无头的镀铬
用户代理:Mozilla/5.0(Windows NT 10.0; Win64; x64)AppleWebKit/537.36(KHTML,例如Gecko)HeadlessChrome/69.0.3497.100Safari/537.36
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
如您所见,无头铬剂称为: HeadlessChrome
As you can see the headless chrome agent is called: HeadlessChrome