How to execute class in logged in slenium instead of opening new chrome instance?

核能气质少年 提交于 2020-02-25 22:42:29

问题


from selenium import webdriver
from time import sleep
filename = "log.txt"
myfile = open(filename, 'w')

class Search(object):
    def __init__(self):
        self.driver = webdriver.Chrome('chromedriver.exe') 

        # "This will open a new chrome instance without being logged in to the site"

        self.driver.get("Site2")
        sleep(2)
        self.driver.find_element_by_xpath("/html/body/div[1]/div[4]/div/div/div[3]/div/div/div[2]/div[2]/div[2]/div/div[4]/div[1]/div[2]/div[1]/a").click()
        sleep(2)
        Texto = self.driver.find_element_by_xpath("/html/body/div[1]/div[4]/div/div/div[4]/div/div/div[1]/div[3]/div/article[1]/div/div[2]/div/div/div/article/div[1]").text
        print(Texto)
        myfile.write(Texto)
        myfile.close()
        sleep(10)
        import re
        Id = re.compile('[0-9]{9}')
        Id2 = re.compile('[0-9]{4}')
        DobMonth = re.compile('[0-9]{2}')
        DobYear = re.compile('[0-9]{2}')
        if Id.match(Texto) and Id2.match(Texto) and DobMonth.match(Texto) and DobYear.match(Texto):
            print ("Matches")
        else:
            print("Not match")
            sleep (20)
            Search()

class BasicBot(object):
    def __init__(self, username, pw):
        self.driver = webdriver.Chrome('chromedriver.exe')
        self.driver.get("site1")
        sleep(2)
        self.driver.find_element_by_xpath("/html/body/div[1]/div[1]/div/div/div/div[1]/a[1]/span").click()
        sleep(2)
        self.driver.find_element_by_xpath("//input[@name=\"login\"]")\
        .send_keys(username)
        self.driver.find_element_by_xpath("//input[@name=\"password\"]")\
        .send_keys(pw)
        self.driver.find_element_by_xpath('/html/body/div[4]/div/div[2]/div/form/div[1]/dl/dd/div/div[2]/button').click()
        sleep(2)
        Search()


BasicBot('username',"password")

So the script runs BasicBot(usr,psswd) logs in to the site, and is supposed to go to a different site while logged in, then searches if X post matches the criteria given, if it does that's it if it does not, I should refresh the site and check again.


回答1:


In the Search class, you start with creating a new chromedriver instance: self.driver = webdriver.Chrome('chromedriver.exe'), this is why it opens another window with a fresh state.

Instead, modify your Search class to

  1. take an existing instance of webdriver.
  2. open a new window by using script window.open instead of get:
class Search:
    def __init__(self, driver):
        self.driver = driver
        # Open a new window
        self.driver.execute_script("window.open('https://site2')")
        sleep(2) # visually check to make sure it opened

        # Switch to new window
        self.driver.switch_to.window(self.driver.window_handles[-1])
        # After this you can start doing self.driver.find_element_by_xpath and the rest

In the class BasicBot, modify the last line to pass the driver:

Search(self.driver)

Lastly, you can use refresh to refresh your site2:

else:
    print("Not match")
    sleep(20)
    self.driver.refresh()

Hope, it helps. Good luck!



来源:https://stackoverflow.com/questions/60364980/how-to-execute-class-in-logged-in-slenium-instead-of-opening-new-chrome-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!