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 protected]")
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.
CodePudding user response:
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.
CodePudding user response:
You accidentally wrote a call to print()
inside the parentheses of .until()
Both at line 23
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "username"))
print("Page loaded!")
)
and at line 37
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "compose-delivery-link"))
print("Compose delivery page loaded!")
)
Additionally, you're gonna want to import the TimeoutException
from selenium.common.exceptions
, because that's likely the next error you will encounter.
And to give some side Information, WebDriverWait
doesn't actually wait 10 seconds before looking for the element. It rather checks for the condition (here the presence of an element with ID "name" or "password" respectively) in a set interval until it either finds the element, in which case the element is returned, or the time limit (here 10 seconds) is reached, in which case it throws a TimeoutException
.