硒脚本不再工作了

时间:2016-09-23 13:00:09

标签: python selenium firefox path geckodriver

我以前在Python中有一个用于firefox网站的selenium脚本代码,运行正常。过了一段时间我更新了Firefox(48)和selenium 2.9.1.1,python是3.5.0

代码,

    # -*- coding: utf-8 -*-
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support.ui import Select
    from selenium.common.exceptions import NoSuchElementException
    from selenium.common.exceptions import NoAlertPresentException
    import unittest, time, re, os

    class Jqueryx(unittest.TestCase):
        def setUp(self):
            self.driver = webdriver.Firefox()
            self.driver.implicitly_wait(30)
            self.base_url = "http://127.0.0.1:8080/"
            self.verificationErrors = []
            self.accept_next_alert = True
            self.path_upload_quest = r'C:\\Users\jl\Documents\DONNEES\DONNEES_LIMONDE'
            self.path_upload_ref = r'C:\\Users\jl\Documents\DONNEES\DONNEES_LIMONDE\ref'

        def test_jqueryx(self):

            driver = self.driver
            driver.get(self.base_url + "/lim/home.php")

如果我现在运行脚本,我收到了这条消息:

  

os.path.basename(self.path),self.start_error_message)   selenium.common.exceptions.WebDriverException:消息:'geckodriver'>可执行文件需要在PATH中。

所以我下载了这个geckodriver的东西并尝试将它添加到python中,但还没有任何效果,

我尝试在脚本中添加它;

os.environ [“PATH”] + = r'C:\ Users \ jl \ geckodriver'

没有成功或在site-package文件夹中添加.pth文件但没有更改...

如何才能让此脚本重回正轨?

THX

2 个答案:

答案 0 :(得分:0)

将GeckoDriver复制到您的代码所在的文件夹中,然后在代码中更改以下内容

self.driver = webdriver.Firefox("path/to/your/current/folder")

这应该很好

答案 1 :(得分:0)

您可以执行以下操作之一:

第一种方法: 将驱动程序放在PATH环境变量中,然后运行脚本

第二种方法(测试期间添加到路径): 通过运行在测试过程中添加驱动程序

os.environ["PATH"] += r'/path/to/dir/where/your/driver/is'

在您的情况下:

os.environ["PATH"] += r'C:\Users\jl'

此代码必须在调用前 执行 浏览器= webdriver.Firefox()

第三种方法(驱动程序的完整路径):

browser = webdriver.Firefox(executable_path=r'/full/path/to/driver')

在您的情况下:

browser = webdriver.Firefox(executable_path=r'C:\Users\jl\geckodriver')
相关问题