2013-08-17 02:11

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


Title says it all. I'm using webdriver on a remote webserver without a GUI. 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))
browser = webdriver.Firefox()

I.e., I can navigate the web, get urls, get source code, and everything else with the browser instance. 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. Everything in the script works up until browser = webdriver.Firefox() - that is, the imports and display stuff all work fine. 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?


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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • douyun8885 douyun8885 8年前

    Solved it by changing the permissions of the folder containing the Python script that called the webdriver from 755 to 757 (i.e., I gave write permissions to all users). 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.

    点赞 评论 复制链接分享