我有一个在pi模型3B+上运行的selenium脚本,该脚本在使用/usr/bin/python/home/pi/main.py手动运行时工作正常,但当使用crontab或LXDE-pi/autostart运行时,它只是启动浏览器,而不继续做它应该做的事情。我尝试使用日志库记录信息,但没有看到任何有用的信息。一开始我以为这是因为selenium没有安装在sudo中,但是当python使用sudo启动时它也可以工作。
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium import webdriver
import logging
logging.basicConfig( level=logging.DEBUG)
#Set chromedriver to kiosk mode and disable
#info popup of "Chrome is being run by test software"
chromeOptions = Options()
chromeOptions.add_argument('--no-sandbox')
chromeOptions.add_argument("--kiosk")
chromeOptions.add_experimental_option("excludeSwitches", ["enable-automation"])
chromeOptions.add_experimental_option('useAutomationExtension', False)
#Disable the save password popup
chromeOptions.add_experimental_option('prefs', {
'credentials_enable_service': False,
'profile': {
'password_manager_enabled': False
}
})
#Initiate chrome driver with set options
driver = webdriver.Chrome(chrome_options=chromeOptions)
#Set implicit wait time to make sure everything is loaded, default is 0
driver.implicitly_wait(30) # seconds
#Navigate to URL
driver.get(url)
#Name of mail form on login page
mail_name = "loginfmt"
#Wait until element is present and interactable
WebDriverWait(driver, 120).until(EC.element_to_be_clickable((By.NAME, mail_name)))
mail_form = driver.find_element_by_name(mail_name)
#Fill out the email and hit enter
mail_form.send_keys(usr)
mail_form.send_keys(Keys.RETURN)
#Name of password form on login page
pass_name = "passwd"
#Wait until element is present and interactable
WebDriverWait(driver, 120).until(EC.element_to_be_clickable((By.NAME, pass_name)))
pass_form = driver.find_element_by_name(pass_name)
#Fill out the password and hit enter
pass_form.send_keys(passwd)
pass_form.send_keys(Keys.RETURN)
下面是日志文件
的输出DEBUG:root:Adding chrome options
DEBUG:root:Disabling the save password popup
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:38727/session {"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"excludeSwitches": ["enable-automation"], "prefs": {"profile": {"password_manager_enabled": false}, "credentials_enable_service": false}, "extensions": [], "useAutomationExtension": false, "args": ["--kiosk"]}, "browserName": "chrome", "platformName": "any"}, "firstMatch": [{}]}, "desiredCapabilities": {"goog:chromeOptions": {"excludeSwitches": ["enable-automation"], "prefs": {"profile": {"password_manager_enabled": false}, "credentials_enable_service": false}, "extensions": [], "useAutomationExtension": false, "args": ["--kiosk"]}, "platform": "ANY", "browserName": "chrome", "version": ""}}
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): 127.0.0.1
DEBUG:urllib3.connectionpool:http://127.0.0.1:38727 "POST /session HTTP/1.1" 200 423
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:root:Adding chrome options
DEBUG:root:Disabling the save password popup
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:35557/session {"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"excludeSwitches": ["enable-automation"], "prefs": {"profile": {"password_manager_enabled": false}, "credentials_enable_service": false}, "extensions": [], "useAutomationExtension": false, "args": ["--kiosk"]}, "browserName": "chrome", "platformName": "any"}, "firstMatch": [{}]}, "desiredCapabilities": {"goog:chromeOptions": {"excludeSwitches": ["enable-automation"], "prefs": {"profile": {"password_manager_enabled": false}, "credentials_enable_service": false}, "extensions": [], "useAutomationExtension": false, "args": ["--kiosk"]}, "platform": "ANY", "browserName": "chrome", "version": ""}}
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): 127.0.0.1
DEBUG:urllib3.connectionpool:http://127.0.0.1:35557 "POST /session HTTP/1.1" 200 423
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:root:Adding chrome options
DEBUG:root:Disabling the save password popup
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:34903/session {"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"excludeSwitches": ["enable-automation"], "prefs": {"profile": {"password_manager_enabled": false}, "credentials_enable_service": false}, "extensions": [], "useAutomationExtension": false, "args": ["--kiosk"]}, "browserName": "chrome", "platformName": "any"}, "firstMatch": [{}]}, "desiredCapabilities": {"goog:chromeOptions": {"excludeSwitches": ["enable-automation"], "prefs": {"profile": {"password_manager_enabled": false}, "credentials_enable_service": false}, "extensions": [], "useAutomationExtension": false, "args": ["--kiosk"]}, "platform": "ANY", "browserName": "chrome", "version": ""}}
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): 127.0.0.1
DEBUG:urllib3.connectionpool:http://127.0.0.1:34903 "POST /session HTTP/1.1" 200 423
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:root:Adding chrome options
DEBUG:root:Disabling the save password popup
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:40785/session {"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"excludeSwitches": ["enable-automation"], "prefs": {"profile": {"password_manager_enabled": false}, "credentials_enable_service": false}, "extensions": [], "useAutomationExtension": false, "args": ["--kiosk"]}, "browserName": "chrome", "platformName": "any"}, "firstMatch": [{}]}, "desiredCapabilities": {"goog:chromeOptions": {"excludeSwitches": ["enable-automation"], "prefs": {"profile": {"password_manager_enabled": false}, "credentials_enable_service": false}, "extensions": [], "useAutomationExtension": false, "args": ["--kiosk"]}, "platform": "ANY", "browserName": "chrome", "version": ""}}
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): 127.0.0.1
DEBUG:urllib3.connectionpool:http://127.0.0.1:40785 "POST /session HTTP/1.1" 200 423
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
我添加的"crontab -e -u pi":
@reboot python /home/pi/main.py
我发现我不能使用SSH启动脚本并让它出现在屏幕上,这使我想到了帮助我找到解决方案的想法https://raspberrypi.stackexchange.com/questions/101126/how-to-schedule-in-crontab-a-python-script-with-selenium-driver-to-show-chromiu
你基本上只需要指定它需要显示在哪个屏幕上!希望它能帮助别人,我花了几个小时才找到这个解决方案,这是如此简单!!!!
crontab -e
@reboot env -i DISPLAY=:0.0 XAUTHORITY="$XAUTHORITY" /usr/bin/python2.7 /home/pi/test.py