【问题标题】:Where to place PhantomJS exe?在哪里放置 PhantomJS exe?
【发布时间】:2018-04-23 06:27:05
【问题描述】:

我正在尝试将 PhantomJS 与 Selenium 和 Python 一起使用。

我的理解是:

我将不得不使用 Selenium 包编写 Python 脚本,该包将与 Selenium 交互以在 PhantomJS WebDriver 上运行以自动化 Web 应用程序测试。

我已经安装了以下:

  • Python v3.5.1.
  • Selenium 使用 pip install selenium v3.7.0。
  • PhantomJS v2.1.1

与此同时,我通过将 Chrome WebDriver 放置在 PATH 中进行了测试,它执行时没有错误。以下是我使用 chrome webdriver 打开 google.com 的脚本。

from selenium import webdriver

driver = webdriver.Chrome() # or add to your PATH
driver.get('https://google.com/')

使用 PhantomJS:

from selenium import webdriver

url = "http://www.google.com"
path_phantom = r'H:\phantomjs\bin\phantomjs.exe'

driver = webdriver.PhantomJS(executable_path=path_phantom)
driver.get(url)
driver.save_screenshot(r'H:\out.png')
driver.quit()

错误:

Traceback(最近一次调用最后一次): 文件“C:\Users\acer\Desktop\testing\openYoutube.py”,第 5 行,在 驱动程序 = webdriver.PhantomJS() 文件“C:\Users\acer\AppData\Local\Programs\Python\Python35-32\lib\site-package s\selenium\webdriver\phantomjs\webdriver.py",第 51 行,在 init 中 log_path=service_log_path) 文件“C:\Users\acer\AppData\Local\Programs\Python\Python35-32\lib\site-package s\selenium\webdriver\phantomjs\service.py",第 50 行,在 init 中 service.Service.init(self, executable_path, port=port, log_file=open(log _path, 'w')) PermissionError: [Errno 13] Permission denied: 'ghostdriver.log'

我是否放错了 PhantomJS exe 或缺少任何步骤?

【问题讨论】:

  • 将它放在任何地方,但只需确保它在环境路径中可用并且您会收到权限错误,因此无论它在哪里出现,它都无法创建日志文件我建议将它放在这样的目录中不需要管理员权限的地方

标签: python-3.x selenium webdriver phantomjs


【解决方案1】:

您可以将PhantomJS v2.1.1 二进制文件放在系统中的任何位置并使用以下代码块:

from selenium import webdriver

url = "http://www.url.com.br/contact.asp"
path_phantom = r'C:\your_path\phantomjs-2.1.1-windows\bin\phantomjs.exe'

driver = webdriver.PhantomJS(executable_path=path_phantom)
driver.set_window_size(1400,1000)
driver.get(url)

更新:

请考虑以下几点并尝试以下代码块和调试消息:

  • 运行 CCleaner 工具以清除系统中的所有 OS 杂务。
  • 您可以选择System Reboot
  • 尝试将Python 应用程序、WebBrowser 二进制文件和WebDriver 二进制文件(即phantomjs.exe)保留在同一驱动器上。

    from selenium import webdriver
    
    url = "http://www.google.com"
    path_phantom = r'C:\Utility\phantomjs-2.1.1-windows\bin\phantomjs.exe'
    
    driver = webdriver.PhantomJS(executable_path=path_phantom)
    print("PhantomJS browser invoked")
    driver.get(url)
    print("Browser Initialized")
    driver.save_screenshot("C://Utility//out.png")
    driver.quit()
    print("Browser Closed")
    

【讨论】:

  • 我对使用所有这些工具的理解是否正确?
  • 嗯,是的。但是当我继续使用不同版本的webdriver 二进制文件时,我建议明确一点。在使用 ChromeGeckoIEPhantomJS WebDrivers 时,我们可以依靠 OS 来帮助您找到 webdriver 二进制文件,我们也可以帮助自己指定绝对路径。
  • 我听从了你的回答。但问题依然存在。
  • @Rahul 你能用你当前的代码块和完整的错误堆栈跟踪更新问题吗?
  • 现在检查。错误和以前一样。
【解决方案2】:

问题似乎出在日志文件上。

更改日志文件路径解决了这个问题。

path_phantom = r'H:\phantomjs\bin\phantomjs.exe'
log_path=r'H:\ghostdriver.log'  #changed path to a temporary file.
# service_log_path is required to change path of log file.
driver = webdriver.PhantomJS(executable_path=path_phantom,service_log_path=log_path) 

【讨论】:

    【解决方案3】:

    根据您的错误:

    PermissionError: [Errno 13] Permission denied: 'ghostdriver.log

    似乎它尝试创建此文件ghostdriver.log 但由于权限而失败。

    按照in this answer的建议,尝试添加参数

    service_log_path=os.path.devnull 
    

    到函数webdriver.PhantomJS()

    或者确保它能够创建文件。

    【讨论】:

      猜你喜欢
      • 2014-11-08
      • 2011-06-29
      • 2018-03-27
      • 2011-02-15
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多