There are two ways to use Headless Chromium:
1. Chrome Headless(使用浏览器自带的headless模式运行)
Usage via the DevTools remote debugging protocol
2.WebDriver
Selenium.WebDriver.ChromeDriver
Chrome Headless
Usage via the DevTools remote debugging protocol
Chrome 的“无头浏览器”模式,是指在没有 X 等图形桌面的环境下,执行完整的 Chrome 功能,通过命令行启动 Chrome 时,带上参数 headless 可以以“无头浏览器”模式启动。一般,直接在命令行使用,我们只是配合 screenshot 完成截图。
step 1.Start a normal Chrome binary with the --headless command line flag (Linux-only for now):
$ chrome --headless --remote-debugging-port=9222 https://chromium.org
Windows 10
在Chrome的桌面快捷方式中添加调试启动参数。 方法:快捷方式–右键属性–目标 在最后添加 --remote-debugging-port=9222即可,和.exe之间有个空格。
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --headless --remote-debugging-port=9222 https://www.baidu.com
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222 http://basic.10jqka.com.cn/603906/operate.html#stockpage
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --headless --remote-debugging-port=9222 http://basic.10jqka.com.cn/603906/operate.html#stockpage
Currently you'll also need to use --disable-gpu to avoid an error from a missing Mesa library.
Navigate to http://localhost:9222 in another browser to open the DevTools interface or use a tool such as Selenium to drive the headless browser.
see:https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md
WebDriver(ChromeDriver)
WebDriver is an open source tool for automated testing of webapps across many browsers. It provides capabilities for navigating to web pages, user input, JavaScript execution, and more. ChromeDriver is a standalone server that implements the W3C WebDriver standard. ChromeDriver is available for Chrome on Android and Chrome on Desktop (Mac, Linux, Windows and ChromeOS).
Chrome / Chromium 有单独的 WebDriver 实现.
ChromeDriver对应已安装的 Chrome 版本,下载 WebDriver 之后,是一个可执行文件。
这个可执行文件的作用,实际上是在本机实现了一套 HTTP 服务,默认在 9515 端口上.
Selenium <-->chromedriver<--> chorme.exe
see:https://sites.google.com/corp/chromium.org/driver/
WebDriver 控制 Chrome Headless
代码直接执行,不开 headless 选项情况下,执行完之后, Chrome 实例会销毁。如果使用 Remote 的方式连到 9515 ,则实例不会销毁,同时可以通过 session_id 属性得到会话标识。
Google account login
I have a problem with Google login. I want to login to my account but Google says that automation drivers are not allowed to log in.
step 1:download ChromeDriver - WebDriver for Chrome
download url: https://sites.google.com/a/chromium.org/chromedriver/downloads
Supports Chrome version 93
Supports Chrome version 92
Gmail Automatic Login via C# (Selenium) in Visual Studio
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace Cube
{
[TestClass]
public class Setup
{
static IWebDriver driverGC;
[AssemblyInitialize]
public static void SetUp(TestContext context)
{
driverGC = new ChromeDriver();
}
[TestMethod]
public void TestChromeDriver()
{
driverGC.Navigate().GoToUrl("https://google.com");
driverGC.FindElement(By.Id("gb_70")).Click();
driverGC.FindElement(By.CssSelector("#identifierId.whsOnd.zHQkBf")).SendKeys("[email protected]");
driverGC.FindElement(By.CssSelector("#identifierId.whsOnd.zHQkBf")).Click();
driverGC.FindElement(By.CssSelector(".whsOnd.zHQkBf")).SendKeys("password123");
driverGC.FindElement(By.CssSelector(".whsOnd.zHQkBf")).Click();
driverGC.Navigate().GoToUrl("https://google.com");
}
}
}
How to automate gmail login process using selenium webdriver in java
https://www.edureka.co/community/2137/automate-gmail-login-process-using-selenium-webdriver-java
Below code worked fine for me (just change the email id n password from your end)
package GmailDemos;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class gmailLogin {
public static void main(String[] args) {
// TODO Auto-generated method stub
gmailLogin obj = new gmailLogin();
obj.loginBrowser();
}
public void loginBrowser() {
System.setProperty("webdriver.firefox.marionette","D:\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(15,TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.get("http://www.gmail.com");
driver.findElement(By.id("identifierId")).sendKeys("[email protected]");
driver.findElement(By.xpath(".//*[@id='identifierNext']/div[2]")).click();
driver.findElement(By.xpath(".//*[@id='password']/div[1]/div/div[1]/input")).sendKeys("password");
driver.findElement(By.xpath(".//*[@id='passwordNext']/div[2]")).click();
}
}
https://www.geeksforgeeks.org/gmail-login-using-python-selenium/
Selenium Google Login Block
However, to help protect your account, Web Browsers may not let you sign in from some browsers. Google might stop sign-ins from browsers that:
Doesn't support JavaScript or have Javascript turned off.
Have AutomationExtension or unsecure or unsupported extensions added.
Use automation testing frameworks.
Are embedded in a different application.
Optimization
Google can detect you are using some program to do the search if Nginx is alway contacting the same Google server. If detected, you will be asked to enter verification code to verify you are a human. To prevent being detected, we can add upstream directive in Nginx server block config file.
upstream www.google.com {
server 172.217.0.228:443;
server 172.217.3.4:443;
server 172.217.4.196:443;
server 172.217.4.132:443;
server 216.58.219.228:443;
server 216.58.192.4:443;
server 216.58.194.196:443;
server 216.58.218.100:443;
server 216.58.219.36:443;
server 216.58.194.68:443;
server 216.58.221.132:443;
server 216.58.221.228:443;
server 216.58.203.4:443;
server 216.58.197.100:443;
server 216.58.199.100:443;
}
server {
listen 80;
........
........
}
server {
listen 443 ssl http2;
......
......
}
see:https://www.linuxbabe.com/nginx/set-nginx-reverse-proxy-google-com
https://stackoverflow.com/questions/60117232/selenium-google-login-block
Unable to sign into google with selenium automation because of “This browser or app may not be secure.”
https://stackoverflow.com/questions/59514049/unable-to-sign-into-google-with-selenium-automation-because-of-this-browser-or
Selenium with C# Tutorial step by step(.net framework)