简体   繁体   English

Python:无法通过脚本启动Selenium Webdriver(Firefox),但可以通过命令行运行

[英]Python: Can't start Selenium Webdriver (Firefox) via a script, but it works via command line

Title says it all. 标题说明了一切。 I'm using webdriver on a remote webserver without a GUI. 我在没有GUI的远程Web服务器上使用WebDriver。 From the command line, I can do this and everything works fine: 在命令行中,我可以执行此操作,并且一切正常:

from selenium import webdriver
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800,600))
display.start()
browser = webdriver.Firefox()

Ie, I can navigate the web, get urls, get source code, and everything else with the browser instance. 即,我可以使用浏览器实例浏览网络,获取URL,获取源代码以及其他所有内容。 But trying to execute a script on the server containing the same lines doesn't work. 但是尝试在包含相同行的服务器上执行脚本不起作用。 It might be relevant that the script is executed via PHP. 通过PHP执行脚本可能与之相关。 Everything in the script works up until browser = webdriver.Firefox() - that is, the imports and display stuff all work fine. 直到browser = webdriver.Firefox()为止,脚本中的所有内容都可以正常工作,也就是说,导入和显示内容都可以正常工作。 It's just that last line; 只是最后一行; for some reason, the browser doesn't want to open when a script tries to do it. 由于某些原因,脚本尝试执行此操作时,浏览器不希望打开。 This makes no sense and I'm really stumped. 这没有任何意义,我真的很沮丧。 Possible reasons/fixes? 可能的原因/解决方法? Maybe something to do with permissions? 也许与权限有关?

EDIT 编辑

Just to clarify, I'm doing this all on the remote server. 为了澄清起见,我正在远程服务器上进行所有操作。 By command line I'm using SSH with PUTTY, and when I try testing the script, I simply do it on the website. 通过命令行,我将SSH与PUTTY结合使用,并且当我尝试测试脚本时,只需在网站上进行操作即可。

Solved it by changing the permissions of the folder containing the Python script that called the webdriver from 755 to 757 (ie, I gave write permissions to all users). 通过将包含名为webdriver的Python脚本的文件夹的权限从755更改为757(即,我为所有用户提供了写权限)来解决该问题。 I'm not sure whether this was a quirk of my situation because of something else I overlooked or whether there's actual insight to be gained here, but I'd say the take home lesson is if weird stuff is happening when working on an unfamiliar server, check the permissions. 我不确定这是否是我的状况的怪癖,是因为我忽略了其他原因,还是在这里可以获得实际的见解,但是我想说的是,在不熟悉的服务器上工作时是否发生了奇怪的事情,检查权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可以通过命令行连接,但不能通过PHP脚本连接 - Can connect via command line but not via a PHP script 脚本导致python通过命令行运行时不会发生的错误 - Script causes errors that don't happen when python run via command line 为什么PHP脚本无法通过命令行或任务计划程序在Server 2008上写入文件? - Why can’t PHP script write a file on server 2008 via command line or task scheduler? CURL 请求通过命令行工作,但不在 PHP 中 - CURL request works via command line but not in PHP cURL通过命令行工作,但不在PHP Curl中工作 - cURL works via command line but not in PHP Curl ffmpeg 命令通过命令行执行,但不通过 PHP 脚本执行 - ffmpeg command executes via command line, but not via PHP script 无法通过 PHP 中的 system() 调用执行 python 脚本 - Can't execute python script via system() call in PHP 通过 PHP 标头进行缓存控制 - Chrome Works / Firefox 没有 - Cache Control via PHP Headers - Chrome Works / Firefox Doesn't 通过命令行使用标头命令运行PHP脚本 - Run a PHP script with a header command via command line Mac OS X上的phantomjs通过命令行工作,而不是通过exec() - phantomjs on Mac OS X works from the command line, not via exec()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM