首页 > 解决方案 > Selenium webdriver 文件(Python)似乎丢失了

问题描述

我是 python 编程的新手,所以请耐心等待!

我最近下载了 Python 3.7.3 以及使用 pip 成功安装了许多附加模块(包括 Numpy、Scipy、Matplotlib、Gamepy、Xlwings 和Selenium)。

使用 Selenium,我正在尝试编写一个非常简单的.py文件来导航到谷歌并进行非常简单的搜索,然后关闭选项卡(我使用Katalon插件记录的代码以及我在下面遇到的问题!)

我的代码:

# -*- 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

class AppDynamicsJob(unittest.TestCase):
    def setUp(self):
        # AppDynamics will automatically override this web driver
        # as documented in https://docs.appdynamics.com/display/PRO44/Write+Your+First+Script
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://www.google.com/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_app_dynamics_job(self):
        driver = self.driver
        driver.get("https://www.google.com/")
        driver.find_element_by_name("q").clear()
        driver.find_element_by_name("q").send_keys("Test search")
        driver.find_element_by_name("q").send_keys(Keys.ENTER)
        driver.close()

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException as e: return False
        return True

    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException as e: return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True

    def tearDown(self):
        # To know more about the difference between verify and assert,
        # visit https://www.seleniumhq.org/docs/06_test_design_considerations.jsp#validating-results
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

出现的错误是

E ================================================== ===================== 错误:test_untitled_test_case(主要.UntitledTestCase) --------- - - - - - - - - - - - - - - - - - - - - - - - - - 追溯(最后一次调用):文件“C:\Users\jer_m\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\service.py”,第 76 行,在 start stdin= PIPE)文件“C:\Users\jer_m\AppData\Local\Programs\Python\Python37\lib\subprocess.py”,第 775 行,在init restore_signals 中,start_new_session)文件“C:\Users\jer_m\AppData\Local\ Programs\Python\Python37\lib\subprocess.py", line 1178, in _execute_child startupinfo) FileNotFoundError: [WinError 2] The system cannot find the file specified

在处理上述异常的过程中,又出现了一个异常:

Traceback(最近一次调用最后一次):文件“C:/Users/jer_m/AppData/Local/Programs/Python/Python37/Scripts/Test.py”,第 12 行,在 setUp self.driver = webdriver.Firefox() 文件中“ C:\Users\jer_m\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\firefox\webdriver.py”,第 164 行,在init self.service.start() 文件中“C: \Users\jer_m\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\service.py”,第 83 行,在 start os.path.basename(self.path), self. start_error_message)selenium.common.exceptions.WebDriverException:消息:“geckodriver”可执行文件需要在 PATH 中。

-------------------------------------------------- -------------------- 在 0.009 秒内运行 1 次测试

失败(错误=1)

引用的大部分 I'm python 文件都是作为Selenium模块一部分的默认文件,因此我不确定路径可能如何更改(例如,我注意到service.py似乎特定于实际驱动程序,所以它在单个浏览器文件夹..)

我正在考虑将service.py文件从 Firefox 文件夹复制到“common”文件夹中。

此外,该subprocess.py文件再次抛出错误,因为这些是库文件,我原以为它们不会引起问题。有没有人经历过这些,如果有,你做了什么来解决这个问题?

里面的代码块subprocess.py是;

    self._execute_child(args, executable, preexec_fn, close_fds,
                        pass_fds, cwd, env,
                        startupinfo, creationflags, shell,
                        p2cread, p2cwrite,
                        c2pread, c2pwrite,
                        errread, errwrite,
                        restore_signals, start_new_session)

    try:
        hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
                                 # no special security
                                 None, None,
                                 int(not close_fds),
                                 creationflags,
                                 env,
                                 os.fspath(cwd) if cwd is not None else None,
                                 startupinfo)

标签: pythonseleniumselenium-webdriverautomationwebdriver

解决方案


推荐阅读