当前位置: 首页 > 知识库问答 >
问题:

Selenium脚本可以手动工作,但不能在启动时工作

闻人修平
2023-03-14

我有一个在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

共有1个答案

能可人
2023-03-14

我发现我无法使用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
 类似资料:
  • 我有一个VB脚本,它有一个相应的.bat文件,把它放在“C:\users\default\appdata\roaming\microsoft\windows\start menu\programs\startup”文件夹中。但是,在启动时,脚本不会完全运行。脚本应该创建任务栏快捷方式,但没有为我列出的Office产品创建快捷方式。 下面是脚本: 下面是.bat文件: 你知道为什么这行不通吗? 答:

  • 我必须得到我使用ffmpeg的视频缩略图。在我的服务器上,我从ssh运行ffmpeg命令,然后它运行正常,但从php exec函数它不运行,它给出错误/usr/bin/ffmpeg:没有这样的文件或目录,但ffmpeg安装在这个位置usr/bin/ffmpeg。我的源代码是: 请提供任何解决方案。

  • 我正在使用Amazon Linux AMI在EC2 AWS上运行以下脚本 从今天起,由于EC2上没有任何更改或新安装,脚本在昨天之前一直工作时停止工作。 本地计算机上的相同脚本仍然有效。 相反,在EC2上会出现以下错误: (节点:12636)UnhandledPromiseRejectionWarning:错误:导航失败,因为浏览器已断开连接!在cdpsession.lifecyclewatche

  • 我想在我的Parallela板启动时运行一个bash脚本,它有Ubuntu。我搜索了SO,在这里找到了说明: 在linux ubuntu下启动时自动运行程序 在Linux上启动时以编程方式运行 我的bash脚本是test。sh,只有一行: 1) 我尝试的第一种方法是添加到脚本的aboslute路径: 2) 我尝试的第二种方法是遵循上面公认的答案。 在这两种情况下,脚本都是在引导后执行的,并且有一个

  • 正在更新属性文件:/home/mehmet/works/netbeansprojects/hsm_java/build/built-clean.属性删除目录/home/mehmet/works/netbeansprojects/hsm_java/build 清洁: 初始化: 已创建目录:/home///netbeansprojects/hsm_java/build/empty 已创建目录:/hom

  • 我创建了一些Selenium IDE脚本,然后将脚本导出Java /Junit4WebDrier支持的,只是简单的WebDrier类。当我运行这些类时,测试用例所做的唯一事情就是打开正确的URL。之后,测试用例是登录测试用例,它根本找不到用户名字段。我尝试通过名称或ID两者都没有工作。我甚至尝试添加一些方法来等待页面加载(实际上加载没有问题);我希望通过名称或id部分工作,至少最后尝试块使用我在网