您如何将无头 Chrome 与 Selenium 一起使用?
How do you use headless Chrome with Selenium?
之所以问这个问题,是因为我想知道在 2021 年做到这一点的最佳方式是什么,因为已经发生了变化,而且我看到人们的做法有所不同。
我希望能够使用 Headless 在后台自动化我的 Twitter 关注机器人。我也是 Python 的新手,仍在学习,所以我很感激代码行。
代码:
import tkinter as tk
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("start-maximized")
ser = Service("C:\Program Files (x86)\chromedriver.exe")
import time
from time import sleep
root = tk.Tk()
app_width = 300
app_height = 320
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width / 2) - (app_width / 2)
y = (screen_height / 2) - (app_height / 2)
root.geometry(f'{app_width}x{app_height}+{int(x)}+{int(y)}')
testbtn_txt = tk.StringVar()
testbtn = tk.Button(root, textvariable=testbtn_txt, command=lambda:open_browser_func(), font="Arial", bg="#808080", fg="white", height=1, width=10)
testbtn_txt.set("Test")
testbtn.grid(row=10, column=0, columnspan=2, pady=5, padx=5)
def open_browser_func():
global driver
driver = webdriver.Chrome(service=ser, options=options)
driver.get("https://twitter.com/i/flow/login")
sleep(5)
wait = WebDriverWait(driver, 30)
loginuser = wait.until(EC.visibility_of_element_located((By.NAME, "text")))
loginuser.send_keys("User", Keys.RETURN)
loginPassword = wait.until(EC.visibility_of_element_located((By.NAME, "password")))
loginPassword.send_keys("Pass", Keys.RETURN)
sleep(10)
driver.get_screenshot_as_file("logged_in_shot.png")
return driver
root.mainloop()
一般来说,您的代码几乎是正确的。
然而,而不是
options.add_argument("start-maximized")
据我所知应该是
options.add_argument("--start-maximized")
此外,我认为最好明确设置 window 大小,例如
options.add_argument("--window-size=1920,1080")
而不是
options.add_argument("--start-maximized")
之所以问这个问题,是因为我想知道在 2021 年做到这一点的最佳方式是什么,因为已经发生了变化,而且我看到人们的做法有所不同。
我希望能够使用 Headless 在后台自动化我的 Twitter 关注机器人。我也是 Python 的新手,仍在学习,所以我很感激代码行。
代码:
import tkinter as tk
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("start-maximized")
ser = Service("C:\Program Files (x86)\chromedriver.exe")
import time
from time import sleep
root = tk.Tk()
app_width = 300
app_height = 320
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width / 2) - (app_width / 2)
y = (screen_height / 2) - (app_height / 2)
root.geometry(f'{app_width}x{app_height}+{int(x)}+{int(y)}')
testbtn_txt = tk.StringVar()
testbtn = tk.Button(root, textvariable=testbtn_txt, command=lambda:open_browser_func(), font="Arial", bg="#808080", fg="white", height=1, width=10)
testbtn_txt.set("Test")
testbtn.grid(row=10, column=0, columnspan=2, pady=5, padx=5)
def open_browser_func():
global driver
driver = webdriver.Chrome(service=ser, options=options)
driver.get("https://twitter.com/i/flow/login")
sleep(5)
wait = WebDriverWait(driver, 30)
loginuser = wait.until(EC.visibility_of_element_located((By.NAME, "text")))
loginuser.send_keys("User", Keys.RETURN)
loginPassword = wait.until(EC.visibility_of_element_located((By.NAME, "password")))
loginPassword.send_keys("Pass", Keys.RETURN)
sleep(10)
driver.get_screenshot_as_file("logged_in_shot.png")
return driver
root.mainloop()
一般来说,您的代码几乎是正确的。
然而,而不是
options.add_argument("start-maximized")
据我所知应该是
options.add_argument("--start-maximized")
此外,我认为最好明确设置 window 大小,例如
options.add_argument("--window-size=1920,1080")
而不是
options.add_argument("--start-maximized")