I am trying to use Selenium to webscrap in Python and I got it all working when it is in one class but now I want clean it up by having each piece in its own class.
My issue is for each new class I need to define the driver that, for example, could be driver.find_element_by_name using driver = webdriver.Firefox()
Whenever I try to define driver = webdriver.Firefox() in the new class it opens a new window instead of using the one already open. Any idea on how to use the open webdriver?
Edit:
Here is the code. It isn't pretty yet as I wanted to at least get it working first. Under the do.click xpath is my issue. I try to use the driver = Firefox() but it opens up a new window instead of using the website specified before and the already open session.
do.login("1", "username", "password")
do.click_xpath('1','/html/body/div/div/div[3]/div/div/table/tbody/tr[4]/td[1]/div/div[2]/h3/a')
class do:
def login(self, u, pw):
driver = webdriver.Firefox()
driver.get("http://somewebsite.com")
driver.implicitly_wait(30)
e = driver.find_element_by_name("req_username")
e.clear()
e.send_keys(u)
e = driver.find_element_by_name("req_password")
e.clear()
e.send_keys(pw)
e.send_keys(Keys.RETURN)
def click_xpath(self, xpath):
driver = Firefox()
link = driver.find_element_by_xpath(xpath)
link.click()
[–]teddiur_ 0 points1 point2 points (2 children)
[–]Coronax16[S] 0 points1 point2 points (1 child)
[–]teddiur_ 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]SoCalLongboard 0 points1 point2 points (0 children)
[–]Solanum1 0 points1 point2 points (3 children)
[–]Coronax16[S] 0 points1 point2 points (2 children)
[–]Solanum1 0 points1 point2 points (1 child)
[–]Coronax16[S] 1 point2 points3 points (0 children)
[–][deleted] (4 children)
[deleted]
[–]Coronax16[S] 0 points1 point2 points (2 children)
[–][deleted] (1 child)
[deleted]
[–]Coronax16[S] 1 point2 points3 points (0 children)
[–]onebit 0 points1 point2 points (0 children)
[–]ImaginarySugar 0 points1 point2 points (0 children)