Selenium and Python if logged in then log out

By Bigfaturd Isbackn

my first time using python and I had a question with some code I wrote.

        # Check if anyone is logged in and if so logs them out
    elem = self.is_element_present(By.XPATH, "//ul[@id='id_name']/li[2]")
    elem1 = driver.find_element_by_css_selector("li.login_li > a > img.rollover").click()
    elem2 = driver.get(self.base_url + "/login")
    if elem.is_displayed():
        elem1
    else:
        elem2

this will check if an account is logged into the site i am testing and if so log them out.
currently it stops at the login page and wont move to the next part.

Full version here:

    # -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class TESTLogin(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://www.force-club.com/"
        self.verificationErrors = []
        self.accept_next_alert = True
    def test_login(self):
        driver = self.driver
        driver.get(self.base_url + "/login")
        # Check if anyone is logged in and if so logs them out
        elem = self.is_element_present(By.XPATH, "//ul[@id='id_name']/li[2]")
        elem1 = driver.find_element_by_css_selector("li.login_li > a > img.rollover").click()
        elem2 = driver.get(self.base_url + "/login")
        if elem.is_displayed():
            elem1
        else:
            elem2
        # Verify Fields
        try: self.assertTrue(self.is_element_present(By.NAME, "uid"))
        except AssertionError as e: self.verificationErrors.append(str(e))
        try: self.assertTrue(self.is_element_present(By.NAME, "pwd"))
        except AssertionError as e: self.verificationErrors.append(str(e))
        try: self.assertTrue(self.is_element_present(By.NAME, "login"))
        except AssertionError as e: self.verificationErrors.append(str(e))
        # Login correct ID & PW
        try: self.assertEqual(u"ログイン | Force Club (フォルスクラブ)", driver.title)
        except AssertionError as e: self.verificationErrors.append(str(e))
        driver.find_element_by_name("uid").clear()
        driver.find_element_by_name("uid").send_keys("")
        driver.find_element_by_name("pwd").clear()
        driver.find_element_by_name("pwd").send_keys("")
        driver.find_element_by_name("uid").clear()
        driver.find_element_by_name("uid").send_keys("*******************")
        driver.find_element_by_name("pwd").clear()
        driver.find_element_by_name("pwd").send_keys("*****************")
        driver.find_element_by_name("login").click()
        try: self.assertEqual(u"マイページ | Force Club (フォルスクラブ)", driver.title)
        except AssertionError as e: self.verificationErrors.append(str(e))
        try: self.assertTrue(self.is_element_present(By.CSS_SELECTOR, "li.login_li > a > img.rollover"))
        except AssertionError as e: self.verificationErrors.append(str(e))
        driver.find_element_by_css_selector("li.login_li > a > img.rollover").click()
        driver.get(self.base_url + "/login")
        # Login no ID no PW
        try: self.assertEqual(u"ログイン | Force Club (フォルスクラブ)", driver.title)
        except AssertionError as e: self.verificationErrors.append(str(e))
        driver.find_element_by_name("uid").clear()
        driver.find_element_by_name("uid").send_keys("")
        driver.find_element_by_name("pwd").clear()
        driver.find_element_by_name("pwd").send_keys("")
        driver.find_element_by_name("login").click()
        try: self.assertEqual(u"ログイン情報が正しくありません。 恐れ入りますが、再度ご確認をお願い致します。", driver.find_element_by_css_selector("#main > p").text)
        except AssertionError as e: self.verificationErrors.append(str(e))
        # Login Correct ID blank PW
        try: self.assertEqual(u"ログイン | Force Club (フォルスクラブ)", driver.title)
        except AssertionError as e: self.verificationErrors.append(str(e))
        driver.find_element_by_name("uid").clear()
        driver.find_element_by_name("uid").send_keys("")
        driver.find_element_by_name("pwd").clear()
        driver.find_element_by_name("pwd").send_keys("")
        driver.find_element_by_name("uid").clear()
        driver.find_element_by_name("uid").send_keys("***************")
        driver.find_element_by_name("login").click()
        try: self.assertEqual(u"ログイン情報が正しくありません。 恐れ入りますが、再度ご確認をお願い致します。", driver.find_element_by_css_selector("#main > p").text)
        except AssertionError as e: self.verificationErrors.append(str(e))
        # Login Blank ID Correct PW
        try: self.assertEqual(u"ログイン | Force Club (フォルスクラブ)", driver.title)
        except AssertionError as e: self.verificationErrors.append(str(e))
        driver.find_element_by_name("uid").clear()
        driver.find_element_by_name("uid").send_keys("")
        driver.find_element_by_name("pwd").clear()
        driver.find_element_by_name("pwd").send_keys("")
        driver.find_element_by_name("pwd").clear()
        driver.find_element_by_name("pwd").send_keys("************")
        driver.find_element_by_name("login").click()
        try: self.assertEqual(u"ログイン情報が正しくありません。 恐れ入りますが、再度ご確認をお願い致します。", driver.find_element_by_css_selector("#main > p").text)
        except AssertionError as e: self.verificationErrors.append(str(e))
        # Login bad ID & PW
        try: self.assertEqual(u"ログイン | Force Club (フォルスクラブ)", driver.title)
        except AssertionError as e: self.verificationErrors.append(str(e))
        driver.find_element_by_name("uid").clear()
        driver.find_element_by_name("uid").send_keys("")
        driver.find_element_by_name("pwd").clear()
        driver.find_element_by_name("pwd").send_keys("")
        driver.find_element_by_name("uid").clear()
        driver.find_element_by_name("uid").send_keys("*************")
        driver.find_element_by_name("pwd").clear()
        driver.find_element_by_name("pwd").send_keys("**************")
        driver.find_element_by_name("login").click()
        try: self.assertEqual(u"ログイン情報が正しくありません。 恐れ入りますが、再度ご確認をお願い致します。", driver.find_element_by_css_selector("#main > p").text)
        except AssertionError as e: self.verificationErrors.append(str(e))
        # Login bad ID Correct PW
        try: self.assertEqual(u"ログイン | Force Club (フォルスクラブ)", driver.title)
        except AssertionError as e: self.verificationErrors.append(str(e))
        driver.find_element_by_name("uid").clear()
        driver.find_element_by_name("uid").send_keys("")
        driver.find_element_by_name("pwd").clear()
        driver.find_element_by_name("pwd").send_keys("")
        driver.find_element_by_name("uid").clear()
        driver.find_element_by_name("uid").send_keys("***************")
        driver.find_element_by_name("pwd").clear()
        driver.find_element_by_name("pwd").send_keys("*****************")
        driver.find_element_by_name("login").click()
        try: self.assertEqual(u"ログイン情報が正しくありません。 恐れ入りますが、再度ご確認をお願い致します。", driver.find_element_by_css_selector("#main > p").text)
        except AssertionError as e: self.verificationErrors.append(str(e))
        # Login Correct ID bad PW
        try: self.assertEqual(u"ログイン | Force Club (フォルスクラブ)", driver.title)
        except AssertionError as e: self.verificationErrors.append(str(e))
        driver.find_element_by_name("uid").clear()
        driver.find_element_by_name("uid").send_keys("")
        driver.find_element_by_name("pwd").clear()
        driver.find_element_by_name("pwd").send_keys("")
        driver.find_element_by_name("uid").clear()
        driver.find_element_by_name("uid").send_keys("*********")
        driver.find_element_by_name("pwd").clear()
        driver.find_element_by_name("pwd").send_keys("**********")
        driver.find_element_by_name("login").click()
        try: self.assertEqual(u"ログイン情報が正しくありません。 恐れ入りますが、再度ご確認をお願い致します。", driver.find_element_by_css_selector("#main > p").text)
        except AssertionError as e: self.verificationErrors.append(str(e))
    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException as e: return False
        return True
    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException as e: return False
        return True
    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
    unittest.main()

Source: Stack Overflow

    

Share it with your friends!

    Fatal error: Uncaught Exception: 12: REST API is deprecated for versions v2.1 and higher (12) thrown in /home/content/19/9652219/html/wp-content/plugins/seo-facebook-comments/facebook/base_facebook.php on line 1273