[英]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.