Untitled

mail@pastecode.io avatarunknown
python
a month ago
5.7 kB
12
Indexable
Never
class Website():
    def __init__(self, driver):
        self.url = 'https://website.com'
        self.driver = driver

    def check_login(self, email, password, username):
        #establish a wait object with a max wait time of 7 seconds
        wait = WebDriverWait(self.driver, timeout=7)
        #open website
        self.driver.get(self.url)
        try:
            #if website/home is opened it means that we are logged in

            wait.until(EC.url_to_be('https://website.com/home'))
            #if we are truly logged in we should be able to find this element
            wait.until(EC.presence_of_element_located((By.XPATH, '//div[contains(@class, "css-1dbjc4n") and contains(@class, "r-1adg3ll") and contains(@class, "r-1ny4l3l")]')))
            #if this element is present it means we are suspended from the website
            try:
                self.driver.find_element(By.XPATH, '//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div/div/div[5]/div/section/div/div/div/div/div[1]/div/div/div/div/h1')
                return 'suspended'
            except:
                return True

        except:
            try:
                #if website.com/home isn't opened it means we are not logged in and we have 2 possibilities
                # 1) website.com opened and we can login 2) an error occured and website.com/404 opened from which we cannot login, if this is the case, refresh page
                if self.driver.current_url != 'https://website.com/':
                    print(self.driver.current_url)
                    self.driver.get(self.url)
                #try to find and click the signin button, if the button is not found it means that a cookies element is blocking it, so we have to accept cookies first
                try:
                    signin_element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="react-root"]/div/div/div[2]/main/div/div/div[1]/div[1]/div/div[3]/div[5]/a/div/span/span')))
                    signin_element.click()
                except:
                    cookies_element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="layers"]/div/div/div/div/div/div[2]/div[2]/div/span/span')))
                    cookies_element.click()
                    signin_element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="react-root"]/div/div/div[2]/main/div/div/div[1]/div[1]/div/div[3]/div[5]/a/div/span/span')))
                    signin_element.click()

                #try to find the mail element, enter email, find the button next and click it to advance
                mail_element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div/div/div/div[5]/label/div/div[2]/div/input')))
                mail_element.send_keys(email)
                next_element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div/div/div/div[6]/div/span/span')))
                next_element.click()

                #sometimes the website asks for your username, sometimes it doesn't, so if there isn't a password element, theres probably a username element first 
                try:
                    password_element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div/div[3]/div/label/div/div[2]/div[1]/input')))
                    password_element.send_keys(password)
                    login_element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[2]/div/div[1]/div/div/div/div/span/span')))
                    login_element.click()
                    return True

                except:
                    username_element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[2]/label/div/div[2]/div/input')))
                    username_element.send_keys(username)                
                    next_element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[2]/div/div/div/div/div/span/span')))
                    next_element.click()
                    #find and enter password, find and click login
                    password_element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div/div[3]/div/label/div/div[2]/div[1]/input')))
                    password_element.send_keys(password)
                    login_element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[2]/div/div[1]/div/div/div/div/span/span')))
                    login_element.click()
                    return True
            except:
                #if there was a problem with any of the previous steps, either something bugged out or the account is suspended
                try:
                    wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div/div/div[5]/div/section/div/div/div/div/div[2]/div/div/div/div/h1/span/span/span')))
                    if self.driver.current_url == 'https://website.com/account/access':
                        return 'suspended'
                except:
                    return False