简体   繁体   中英

Headless mode using selenium is not working

As whatsapp require barcode to scan, I want it to scan for the first time and afterwards it will run in headless mode. Any Suggestion, here's my code?

from selenium import webdriver

from selenium.webdriver.chrome.options import Options

options = Options()

options.headless=True

options.add_argument("headless")

#options.headless = True

options.add_argument("no-sandbox")

options.add_argument("start-maximized")

options.add_argument("window-size=1900,1080");

options = webdriver.ChromeOptions()

#options.add_argument(CHROME_PROFILE_PATH)

browser=webdriver.Chrome(executable_path='C:\\ChromeDriver\\chromedriver_win32\\chromedriver.exe', options=options)

browser.maximize_window()

browser.get('https://web.whatsapp.com/')

You could do a screenshot of the page, then access it, scan it with phone and it should work as if you scanned it in browser. you can use the screenshot function of selenium itself I guess.

Remove options = webdriver.ChromeOptions(), you re assigning empty options object to options identifier again.:

Also add remote debugging port to access ui of headless browser,

Close all chrome before running the script,

from selenium import webdriver

from selenium.webdriver.chrome.options import Options

options = Options()

options.headless=True

options.add_argument("--remote-debugging-port=1559")

options.add_argument(
"user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")

options.add_argument("no-sandbox")

options.add_argument("start-maximized")

options.add_argument("window-size=1900,1080");


browser=webdriver.Chrome(executable_path='C:\ChromeDriver\chromedriver_win32\chromedriver.exe', options=options)

browser.maximize_window()

browser.get('https://web.whatsapp.com/')

input("Scan the bar code and press enter")

Now open another chrome and navigate to:

  http://127.0.0.1:1559

Click the inspectable link:

在此处输入图像描述

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM