我试图驱动无头浏览器登录到我公司使用的基于云的工资单软件。但是它甚至无法超过登录阶段。在“ head”模式下,代码工作正常。以下是我的代码:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("--headless") # Runs Chrome in headless mode.
options.add_argument('--no-sandbox') # Bypass OS security model
# options.add_argument('--disable-gpu') # applicable to windows os only
options.add_argument('start-maximized') #
options.add_argument('disable-infobars')
options.add_argument("--disable-extensions")
browser = webdriver.Chrome(options=options, executable_path='/Applications/chromedriver')
print("Headless Chrome Initialized on MacOS")
# Navigate to website
browser.get('https://sg.quickhr.co')
print('Navigate to quickHR website')
# Login to quickHR
print('Keying in credentials')
# Define credentials and key-in accordingly
email = 'user@user.com'
password = '********'
browser.find_element_by_id('inputName').send_keys(email)
browser.find_element_by_id('inputPassword').send_keys(password)
browser.find_element_by_id('validate_login_lb').click()
print('Logging in')
# Wait for login to complete and webpage and elements to load before proceeding
try:
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'flaticon-payroll')))
finally:
browser.find_element_by_class_name('flaticon-payroll')
以下是我的错误代码:
Traceback (most recent call last):
File "/Users/cadellteng/PycharmProjects/Playground/login.py", line 71, in <module>
browser.find_element_by_class_name('flaticon-payroll').click()
File "/Users/cadellteng/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "/Users/cadellteng/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/Users/cadellteng/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/cadellteng/venv/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: headless chrome=79.0.3945.130)
因此显然是不可交互的。我不确定这是什么意思,因为它在没有头的情况下仍然有效。