Home > Mobile >  Can't use Selenium WebDriver in another file in Python
Can't use Selenium WebDriver in another file in Python

Time:04-13

I have created a method to generate a Webdriver object in WebDriverFactory.py:

#!python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

def generer_chrome_driver():
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
    return driver

And when I want to use this Webdriver object in the another file test.py, no Webdriver methods appear:

#!python
import webdriverFactory

driver = webdriverFactory.generer_chrome_driver
driver. --> no methods

But if I do the same thing directly in test.py, then I have access to all the Webdriver methods like .get(url) :

#!python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get('www.google.fr')

I'm new in Python, I don't know if it's a subtlety of the language that I don't see... Thank you for your help.

CodePudding user response:

I assume you have both files in the same directory (or ˋWebDriver.pyˋ is somewhere else in the ˋPYTHONPATHˋ).

In that case, you may just be missing the parentheses after generer_chrome_driver.

driver = webdriverFactory.generer_chrome_driver()

If you are relying on your IDE to show you the available methods, it may mislead you. Sometimes just the auto completion fails, but you can enter the method name manually, save the file and try to run the script afterwards and it will tell you if the method is really unavailable.

CodePudding user response:

Thank you for your reply user2154065. The problem was exactly the missing of parentheses after generer_chrome_driver... The auto-completion is not does not add the parentheses. And there is no error reporting. This is a mistake I will never make again :P

  • Related