Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Selenium check for text otherwise prompts the next task

On python 3.9 and selenium 4.0.0

So I’m currently trying to go to a website and check if there is text there like "Please enter your username" and if that text is not there, it’ll open up a different tab and go to a different website.

This is my current code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

driver = webdriver.Chrome(ChromeDriverManager().install())  # opens a new chrome window
driver.maximize_window()  # maximizes the window
driver.get('website.com')

if driver.find_element(By.XPATH, '//*[@id="outside"]/div[4]/div/p [contains(text(), "Please enter your Username and Password to continue.")]').click() is True:
    loginBox = driver.find_element(By.XPATH, '//input[@id="username"]') 
    loginBox.send_keys("username") 

    password = driver.find_element(By.XPATH, '//input[@type = "password"]')
    password.send_keys("password")
else:
    pass

driver.execute_script("window.open('about:blank','secondtab');")
driver.switch_to.window("secondtab")
driver.get("secondwebsite.com")
...

It currently just opens the website and sits there while not checking for any of the conditions I wanted.

>Solution :

.click returns type is void, so it would not make any sense to have it returned as true.

Also, if you use find_elements (Plural), you would have a list.

Please use the below code :

try:
    if len(driver.find_elements(By.XPATH, "//*[@id='outside']/div[4]/div/p [contains(text(), 'Please enter your Username and Password to continue.')]")) > 0:
        print("This means that the above xpath is valid and have a node in HTMLDOM")
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='outside']/div[4]/div/p [contains(text(), 'Please enter your Username and Password to continue.')]"))).click()
        loginBox = driver.find_element(By.XPATH, '//input[@id="username"]')
        loginBox.send_keys("username")

        password = driver.find_element(By.XPATH, '//input[@type = "password"]')
        password.send_keys("password")
    else:
        print("Please check this xpath, //*[@id='outside']/div[4]/div/p [contains(text(), 'Please enter your Username and Password to continue.')]")
        pass
except:
    pass

driver.execute_script("window.open('about:blank','secondtab');")
driver.switch_to.window("secondtab")
driver.get("secondwebsite.com")

I have induced Explicit waits (WebDriverWait), Please use below imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

xpath that you should check :

//*[@id='outside']/div[4]/div/p [contains(text(), 'Please enter your Username and Password to continue.')]

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading