当我不在无头模式下运行 chrome 时,我的代码工作得很好,但在无头模式下我得到“元素不可交互”。
我在 email_box.send_keys('') 处收到错误
我已经设置了窗口大小,但它仍然不起作用
代码:
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import time
options = Options()
options.add_argument('headless')
options.add_argument('window-size=1366x768')
with Chrome(options=options) as driver:
driver.get('https://accounts.google.com/login')
WebDriverWait(driver, 20).until(lambda d: d.find_element(By.TAG_NAME, 'input'))
time.sleep(2)
email_box = driver.find_element(By.TAG_NAME, 'input')
time.sleep(2)
email_box.send_keys('example@gmail.com')
Run Code Online (Sandbox Code Playgroud) selenium python-3.x selenium-chromedriver selenium-webdriver google-chrome-headless
with Chrome() as driver:
driver.get(notebooks[0])
for cookie in pickle.load(open('cookies.pkl', 'rb')):
driver.add_cookie(cookie)
Run Code Online (Sandbox Code Playgroud)
我首先通过手动登录转储cookie,但在设置它们时出现此错误。
python selenium setcookie selenium-chromedriver selenium-webdriver