2

I'm new to python programming so bear with me!

I've recently downloaded Python 3.7.3 along with using pip to successfully install a number of add-on modules (including Numpy, Scipy, Matplotlib, Gamepy, Xlwings and Selenium).

With Selenium I'm trying to script a very simple .py file to navigate to google and to do a very simple search and then close the tab (code I've recorded, using the Katalon plugin, and issues I'm experiencing below!)

My code:

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

Errors coming up is

E ====================================================================== ERROR: test_untitled_test_case (main.UntitledTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\Users\jer_m\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start stdin=PIPE) File "C:\Users\jer_m\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 775, in init restore_signals, start_new_session) File "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

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:/Users/jer_m/AppData/Local/Programs/Python/Python37/Scripts/Test.py", line 12, in setUp self.driver = webdriver.Firefox() File "C:\Users\jer_m\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 164, in init self.service.start() File "C:\Users\jer_m\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\service.py", line 83, in start os.path.basename(self.path), self.start_error_message) selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

---------------------------------------------------------------------- Ran 1 test in 0.009s

FAILED (errors=1)

Much of the I'm python files being referenced are the default files as part of the Selenium module so I'm not sure how the paths might have changed (I notice, for example, the service.py seems to be specific to the actual driver so it's in the individual browser folder..)

I was contemplating copying the service.py file from the Firefox folder, into the "common" folder.

Also, the subprocess.py file is throwing up errors and again, as these are library files, I'd have thought they wouldn't cause issues.. Has anybody experienced these and, if so, what have you done t get around this?

The blocks of code within subprocess.py are;

    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)

and

    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)
Jeremy
  • 1,337
  • 3
  • 12
  • 26

0 Answers0