Search code examples
pythonseleniumselenium-webdriverwebdriverwaitexpected-condition

How do I check in Python Selenium if a user is already registered? (Magento Cloud)


Initial situation:

Currently I have defined several test cases in our project, including login and logout within a Magento cloud environment.

I currently used the Chrome Webdriver for this test.

Python and the latest Selenium version.

Problem situation: I would like to check if a user is already registered.

What I have so far:

I'm currently checking whether a user wants to log in, and if it's the corresponding user "Frank" an assertion will be triggered.

But I believe that there are better solutions?

 def test_login(self):
        driver = self.driver 
        time.sleep(10) 
        driver.find_username = wait.until(EC.presence_of_element_located((By.XPATH,'//span[contains(text(), "Frank")]')))
        assert find_username

The question

Are there more sensible solutions how to query this within Python/Selenium?


Solution

  • You were close. Instead of using presence_of_element_located() you need to use visibility_of_element_located() and you can use the following Locator Strategies:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.common.exceptions import TimeoutException
    
    def test_login(self):       
        try:
            driver.find_username = wait.until(EC.visibility_of_element_located((By.XPATH,'//span[contains(., "Frank")]')))
            print("User was found")
            assert find_username
        except TimeoutException:
            print("User wasn't found")