im试图使用chromedriver使用python-selenium登录我的谷歌帐户,代码可以工作,但不能在无头模式下工作。在hm中,我得到了标识符Id从未出现:(
编辑:添加丢失--禁用gpu
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--window-size=1920,1080')
def do_login(email, password):
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://accounts.google.com/signin/v2/identifier?flowName=GlifWebSignIn&flowEntry=ServiceLogin')
email_phone = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='identifierId']")))
email_phone.send_keys(email)
driver.find_element_by_id("identifierNext").click()
pw = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.XPATH, "//input[@name='password']"))
)
pw.send_keys(password)
driver.find_element_by_id("passwordNext").click()
time.sleep(2)
print(driver.title)
driver.close()
应该像在非无头模式中那样登录吗
您还必须将--disable-gpu
添加到铬选项中。
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--window-size=1920,1080')
chrome_options.add_argument('--disable-gpu')
这就是我必须添加的内容,以使我的无头代码完全工作。
我总是传递以下参数,经过测试并工作:
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--window-size=1920,1080')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--start-maximized')
chrome_options.add_argument('--disable-setuid-sandbox')
此代码在无头模式下工作,但不支持启用gui的
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--window-size=1920,1080')
def do_login(email, password):
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://accounts.google.com/signin/v2/identifier?flowName=GlifWebSignIn&flowEntry=ServiceLogin')
email_phone = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "Email")))
email_phone.send_keys(email)
driver.find_element_by_id("next").click()
pw = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.ID, "Passwd"))
)
pw.send_keys(password)
driver.find_element_by_id("signIn").click()
driver.close()