简体   繁体   中英

How to assert/verify successful login using Python / Selenium Webdriver

I have done a little research and could not find any answer specific to Selenium WebDriver with Python. I can successfully sign into the page but I cannot find way(s) to verify that the login was successful. Page title does not work for me since it does not change. Python Selenium documentation does not have any good explanation or examples. All I want to do after this code is to put a line and assert that the username "Tuto" is visible on the page

LoginButtonLocator = "//a[contains(text(), 'Login')]"
facebookConnectButtonLocator = "//a[contains(text(), 'Connect with Facebook')]"

facebookLoginLocatorID = "email"
facebookPasswordLocatorID = "pass"
facebookLoginButtonLocatorID = "loginbutton"

LoginButtonElement = WebDriverWait(driver, 20).until(lambda driver: driver.find_element_by_xpath(LoginButtonLocator))
LoginButtonElement.click()
facebookConnectButtonElement = WebDriverWait(driver, 20).until(lambda driver: driver.find_element_by_xpath(facebookConnectButtonLocator))
facebookConnectButtonElement.click()
facebookLoginElement = WebDriverWait(driver, 20).until(lambda driver: driver.find_element_by_id(facebookLoginLocatorID))
facebookLoginElement.send_keys(facebookID)
facebookPasswordElement = WebDriverWait(driver, 20).until(lambda driver: driver.find_element_by_id(facebookPasswordLocatorID))
facebookPasswordElement.send_keys(facebookPW)
facebookLoginButtonElement = WebDriverWait(driver, 20).until(lambda driver: driver.find_element_by_id(facebookLoginButtonLocatorID))
facebookLoginButtonElement.click()

I am working with the Javascript API instead of the Python version, so the syntax is different, but here is how I would get about it (using mocha as a test framework):

facebookLoginButtonElement.click().then(function(_) {
    driver.findElement(By.xpath('//a[text() = "Tuto"]')).then(function(userLink) {
        assert.ok(userLink);
    });
});

In the javascript version, if <a ...>Tuto</a> can't be found there would be an error before the callback is called, so the assertion would be redundant (would only get there if the link was found), but I find it as self-documenting so I add the assert.

Just try to find an element using xPath:

wait = WebDriverWait(browser, 10) 

find_username = wait.until(EC.presence_of_element_located((By.XPATH,'//span[contains(text(), "Alex")]')))

assert find_username

Hope, it will help you.

I'm writing it considering that you are using webdriver_manager for Chrome, though it doesn't create many effects, You can use the code in both scenarios with webdriver_manager or without as well provided that you have made significant changes in adding executable path for webdriver if not using webdriver_manager . Here you go...

from selenium import webdriver

// this was for importing webdriver

from webdriver_manager.chrome import ChromeDrivermanager
driver = webdriver.Chrome(ChromeDriverManager().install())

driver.get("https://somedummy.domain.com/login")
driver.find_element_by_name("username").send_keys("somedummyvalidated@email.com")
driver.find_element_by_name("password").send_keys("someDummyPass")
driver.find_element_by_id("btnLogin").click()

this block was about sending keys, now if you are logged in correctly then the page title must change. Just use the below code to fetch the title and use assert or match the titles before and after login.

driver.title

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM