问题
I am trying to automate sending a text message using python & selenium from https://voice.google.com/about
. When I run below code, it picks up the lattest version/ instance of google chrome and works fine. But, when I run it headless mode, it uses an older version of google chrome(guessing from screenshot) and gives TimeoutException()
exception. It fails while logging in.
I checked an element id of sign in box in an IE emulator and it appears to be the same.
url = "https://voice.google.com/about"
driver.get(url)
time.sleep(5)
driver.get_screenshot_as_file('C:\\Drivers\\Q11-0.png')
# Click on a GET GOOGLE VOICE
WebDriverWait(driver, 60).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="heroToggle"]/button/span'))).click()
print("Clicked on a get google voice")
# Click on a WEB
WebDriverWait(driver, 60).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="heroToggle"]/div/button[3]'))).click()
print("Clicked on a WEB button.")
driver.get_screenshot_as_file('C:\\Drivers\\Q11-1.png')
# Enter username and password
# THIS IS WHERE IT FAILS
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.ID, 'identifierId')))
email = driver.find_element_by_id('identifierId')
time.sleep(10)
email.send_keys('username')
time.sleep(10)
nextBtn = driver.find_element_by_xpath('//*[@id="identifierNext"]/content/span')
nextBtn.click()
driver.get_screenshot_as_file('C:\\Drivers\\Q11-2.png')
# Enter password
password = driver.find_element_by_xpath('//*[@id="password"]/div[1]/div/div[1]/input')
password.send_keys('password')
nextBtn = driver.find_element_by_xpath('//*[@id="passwordNext"]/content')
nextBtn.click()
Screenshot when I run in normal mode,
Screenshot when I run in headless mode,
Drivers are updated properly. I am not sure what I am doing wrong. Any help would really be appreciated.
回答1:
Try to override user-agent, so it wont contains headless tag.
opts = Options()
opts.add_argument("user-agent=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")
driver = webdriver.Chrome(chrome_options=opts)
来源:https://stackoverflow.com/questions/53828421/python-headless-mode-picks-up-the-older-version-of-chrome