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

How to print messages in console in Python Selenium

I’m using below code to test login to a website.The code opens a webpage and wait for an element to load and then logs in using the provided credentials. I have put some messages in the code for me to identify whether the code is running. However, it is not working.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

options = Options()

options.add_argument("--headless")
options.add_argument("--no-sandbox")

s = Service("/usr/local/bin/chromedriver")
url = "https://atf.domain.com/"
driver = webdriver.Chrome(options=options, service=s)
driver.get(url)
print(driver.title)

try:
# wait 10 seconds before looking for element
element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "username"))
    print("Page loaded!")
)
except TimeoutException:
print("Page not loaded!")


driver.find_element(By.ID, "username").send_keys("email@gmail.com")
driver.find_element(By.ID, "password").send_keys("password")
driver.find_element(By.ID, "signinButton").click()

try:
# wait 10 seconds before looking for element
element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "compose-delivery-link"))
    print("Compose delivery page loaded!")
)
except TimeoutException:
print("Compose delivery page not loaded!")

driver.find_element(By.ID, "compose-delivery-link").click()


driver.close()

I’m getting below error,

File "sft_login_test.py", line 24
print('Page loaded!')
    ^
SyntaxError: invalid syntax

I have no tried idea how to fix this. Any help is greatly appreciated.

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

>Solution :

element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "username"))
)
print("Page loaded!")

I guess the print call should be outside the .until arguments.

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