Selenium和Geckodriver的路径问题

时间:2016-10-30 15:36:53

标签: python selenium path geckodriver

我是编程的新手,做了一些python课程,并试图应用我一直在学习的内容。

我正在运行macOS Sierra并在我的机器上安装了python2和3 ,即使我只是想使用python3,但我之前的课程已经指示我从python2开始,我不知道这是不是坏事。

无论如何,使用Python课程(使用python3)自动化无聊的东西我遇到了这段代码:

#! python3
from selenium import webdriver
browser = webdriver.Firefox()

并收到以下错误消息:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 64, in start
    stdout=self.log_file, stderr=self.log_file)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 947, in __init__
    restore_signals, start_new_session)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 1551, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/Alex/Anaconda/Templates/selenium_firefox.py", line 3, in <module>
    browser = webdriver.Firefox()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 135, in __init__
    self.service.start()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 71, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 

Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x1029777f0>>
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 163, in __del__
    self.stop()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 135, in stop
    if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
[Finished in 0.501s]

我找到了一个似乎可以解决我的问题的答案: Selenium using Python - Geckodriver executable needs to be in PATH

但我不太明白如何操作计算机中的PATH或如何以计算机的工作方式组织我的文件。

我在终端上执行了以下代码(按照其他查询中的说明): 的 exportPATH = $ PATH:/路径/到/目录/的/可执行/下载/中/上/步

但它对我没有任何意义,也没有用。我还尝试从下载(最初的位置)获取Geckodriver文件并将其放在我的Anaconda文件夹中。

无论如何,我很确定问题是我真的不知道计算机是如何组织起来的,因此无法正确处理代码。

因此,我想问一个针对我的具体案例的解决方案,以及我可以用来更好地理解所有这些工作方式的参考文本,教程,视频或类似内容(我仍然没有'在这个问题上找到任何好的材料。)

提前致谢!

3 个答案:

答案 0 :(得分:4)

我有同样的错误,我想出来了,这是步骤:

<强>的Mac:

  1. 从以下链接下载适用于MacOS的geckodriver:

    https://github.com/mozilla/geckodriver/releases

  2. 转到终端并键入以下命令以了解Python的路径:

    @using System.Web.Mvc
    @using System.Web.Mvc.Html
    @using System
    @using System.Web.UI
    @model Dependency_Injection_MEF_MVC.Models.Payment
    
    @section Scripts{
        <script type="text/javascript" src="https://js.stripe.com/v2/"></script>
        <script type="text/javascript">
            Stripe.setPublishableKey('pk_test_6pRNASCoBOKtIshFeQd4XMUh');
        </script>
    
        <script type="text/javascript">
            $(function () {
                var $form = $('#payment-form');
                $form.submit(function (event) {
                    // Disable the submit button to prevent repeated clicks:
                    $form.find('.submit').prop('disabled', true);
    
                    // Request a token from Stripe:
                    Stripe.card.createToken($form, stripeResponseHandler);
    
                    // Prevent the form from being submitted:
                    return false;
                });
            });
    
            function stripeResponseHandler(status, response) {
                // Grab the form:
                var $form = $('#payment-form');
    
                if (response.error) { // Problem!
    
                    // Show the errors on the form:
                    $form.find('.payment-errors').text(response.error.message);
                    $form.find('.submit').prop('disabled', false); // Re-enable submission
    
                } else { // Token was created!
    
                    // Get the token ID:
                    var token = response.id;
    
                    ViewBag.token = token;
    
                    // Insert the token ID into the form so it gets submitted to the server:
                    $form.append($('<input type="hidden" name="Token">').val(token));
    
                    // Submit the form:
                    $form.get(0).submit();
    
                }
            };
        </script>
    }
    
    <div class="row">
        <div class="col-md-12 form-column">
            <div class="form-container">
                <form asp-controller="home" asp-action="processpayment" method="POST" id="payment-form">
                    <span class="payment-errors"></span>
    
                    <div class="form-group">
                        <h3>Membership Amount: USD XX</h3>
                    </div>
    
                    <div class="form-group">
                        <label for="cardNumber">Card Number</label>
                        <input class="form-control form-input" id="cardNumber" type="text" size="20" data-stripe="number" style= "width:250px;height:25px;font-size:120%">
                    </div>
    
                    <div class="form-group">
                        <label>Expiration (MM/YY)</label>
                        <div>
                            <input class="form-control form-input date-input" type="text" size="2" data-stripe="exp_month" style= "width:250px;height:25px;font-size:120%">
                            <input class="form-control form-input date-input" type="text" size="2" data-stripe="exp_year" style= "width:250px;height:25px;font-size:120%">
                        </div>
                    </div>
    
                    <div class="form-group">
                        <label for="cvc">CVC</label>
                        <input class="form-control form-input" id="cvc" type="text" size="4" data-stripe="cvc" style= "width:250px;height:25px;font-size:120%">
                    </div>
    
                    <input class="btn btn-default" onclick="doSomething()" id="submit" value="Submit Payment">
                </form>
            </div>
        </div>
    </div>
    
    @functions{
    
        void doSomething(){
    
            if(ViewBag.token != null){
                        alert("@ViewBag.token");
            }
        }
    
    }
    

    通常路径为 / usr / local / bin

  3. 将geckodriver从downloads文件夹复制到您在步骤2中获得的路径。使用以下命令:

    echo $PATH
    
  4. 注意:有时在执行第3步时,您可能会收到权限被拒绝错误,要解决此错误,您应该在前面使用 sudo 像这样的命令:

    cp downloads /usr/local/bin 
    

    之后您必须输入您的帐户密码。仅供参考,sudo cp downloads /usr/local/bin 使您以管理员身份运行命令。

答案 1 :(得分:1)

您找到的UNIX链接应该可以使用。 $contentMD5Value = base64_encode(md5_file('/path/to/file.txt', true)); export之间是否有空格?您的副本之间没有空格,因此无效。如果它在UNIX中与在Python路径中用于geckodriver的Windows相同,那么您可以尝试:

PATH

然后geckodriver存储在Python的基本路径中,因此会自动初始化。

答案 2 :(得分:1)

您可以随时对潜水员位置进行硬编码:

sudo nano /usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py

def __init__(self, firefox_profile=None, firefox_binary=None,
         timeout=30, capabilities=None, proxy=None,
         executable_path="/PATH/gecko/geckodriver",                     
firefox_options=None,
         log_path="/PATH/geckodriver.log"):