Error closing browser in Selenium-webdriver

Asked

Viewed 94 times

-1

Code:

firefoxbin = os.path.abspath(os.path.join(os.path.dirname(__file__), 'drivers', 'geckodriver'))

assert os.path.isfile(firefoxbin), "Driver \"{}\" não disponível.".format(firefoxbin)
        logger.debug(f"firefoxbin: {type(firefoxbin)}")
        navegador = webdriver.Firefox(executable_path=firefoxbin)
        logger.debug(f"navegador: {type(navegador)}")
        navegador.get('/')
finally:
    navegador.close()

Error:

Exception: 'str' Object has no attribute 'close'

  • Where is the try that finally?

  • I forgot to put the question, but the code is right. I discovered the error and put it right. But in short it was basically disable Marionette.

2 answers

0

The problem here is that your code finally will rotate no matter what happens before. It would be important to look at the output of your code in detail to better understand what is happening.

But one clear problem is that you did the creation of webdriver (variable navegador) and did not check whether it effectively resulted in an object Webdriver.

Since the output was not that object, then the error says that the string returned by the call has no method close(), which in this case belongs to the webdriver.

To test, before the finally, add print navegador and see the return. If the creation has been successful, you will have a return like this:

<selenium.webdriver.firefox.webdriver.WebDriver (session="49785161406c39caa34c0ae34c90a17b")>

Otherwise, see if the path you’re using actually has the driver geckodriver, if the get is not returning any errors, or other problems with the creation / use of navegador before arriving at the finally.

0


In linux the execution occurs carretamento, in windows the compatibility of Marionette not so it was necessary to change the standard of the capabilities disabling the Marionette.

Answer:

firefoxbin = os.path.abspath(os.path.join(os.path.dirname(__file__), 'drivers', 'geckodriver'))

assert os.path.isfile(firefoxbin), "Driver \"{}\" não disponível.".format(firefoxbin)
        logger.debug(f"firefoxbin: {type(firefoxbin)}")

        cap = DesiredCapabilities().FIREFOX
        cap["marionette"] = False
        navegador = webdriver.Firefox(capabilities=cap, executable_path=firefoxbin)

        logger.debug(f"navegador: {type(navegador)}")
        navegador.get('/')
finally:
    navegador.close()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.