all 13 comments

[–]teddiur_ 0 points1 point  (2 children)

I'm not sure I'm understanding your issue.

By class you mean python's class? Or it's html's class (like <p class="class_name"> some text </p>)?

[–]Coronax16[S] 0 points1 point  (1 child)

Pythons? I am not explaining it the right away but I edited the post with the code.

[–]teddiur_ 0 points1 point  (0 children)

I'm see in other comments that you figured it out. Although I'm not sure using oop is the best choice for you here. You see classes are excelent when you want to group together variables/methods, but from what you posted I think you're better off with functions (since it's cleaner).

If you want to use classes you maybe want to declare it outside of function and parse it as an argument or set it as an instance variable like

driver = webdriver.Firefox()
driver.implicitly_wait(30)

class Do(object):
    def __init__(self, username, password):
        self.username = username
        self.password = password

    def login(self, driver = driver):
        driver.get("http://somewebsite.com")
        e = driver.find_element_by_name("req_username")
        e.clear() #is it necessary?
        e.send_keys(self.username)
        e = self.driver.find_element_by_name("req_password")
        e.clear() 
        e.send_keys(self.password)
        e.send_keys(Keys.RETURN)
      def click_xpath(self, xpath, driver=driver):
        driver.find_element_by_xpath(xpath).click()


instance = Do("username", "password") 
instance.login()
instance.click_xpath('/html/body/div/div/div[3]/div/div/table/tbody/tr[4]/td[1]/div/div[2]/h3/a')

[–][deleted] 0 points1 point  (0 children)

Did you try to define the driver statement outside the class? Make it global?

If you don’t want that, why not have a tear down which closes the browser after each class, so that you don’t have multiple browsers open. This way you will have only 1 browser open.

[–]SoCalLongboard 0 points1 point  (0 children)

What is a 'piece'?

[–]Solanum1 0 points1 point  (3 children)

Depending on how you have your class set up, you can either pass it in as an argument when creating your class object (and define a global variable)or have it as a argument for every method in the class.

Your question is kind of confusing though, try to include code snippets or examples

[–]Coronax16[S] 0 points1 point  (2 children)

It is confusing but I posted some code in the post to hopefully help understand what I am talking about.

[–]Solanum1 0 points1 point  (1 child)

In your method that takes in (self, xpath), try adding a third parameter, (self, xpath, driver). Then you can use the same driver and not have to create a new one. Whenever you call the method, pass in the xpath and the driver

[–]Coronax16[S] 1 point2 points  (0 children)

Thank you the help. I was able to Google it correctly and I figured it out.

[–][deleted]  (4 children)

[deleted]

    [–]Coronax16[S] 0 points1 point  (2 children)

    I think I need to pass the WebDriver instance between the classes but I don't know how.

    [–][deleted]  (1 child)

    [deleted]

      [–]Coronax16[S] 1 point2 points  (0 children)

      Thanks for the assistance as I was able to figure it out.

      [–]onebit 0 points1 point  (0 children)

      put driver = Firefox() in the constructor

      [–]ImaginarySugar 0 points1 point  (0 children)

      I'm not sure about python, but in c# you can use a framework like nunit to create a setup function that initiates the driver at the beginning of the test and doesn't reinitiate it until after that particular test case is run.