When i was testing my program, it’s end with code 1
- here is full error (
"Traceback (most recent call last): File "C:Usersds072PycharmProjectspythonProjectmain.py", line 20, in <module> wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/main/div/div/div/div[1]/div[3]/div[1]/div[1]/input"))).send_keys("my username"
)”) - here is my code (https://pastebin.com/RThnY8GY)
I have tested alternatives for this value (
"/html/body/main/div/div/div/div[1]/div[3]/div[1]/div[1]/input"
) but with out results :C
Please help me
Advertisement
Answer
The following is one way to select those fields and login into that website (fields are in an iframe):
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC chrome_options = Options() chrome_options.add_argument("--no-sandbox") chrome_options.add_argument("window-size=1280,720") webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary browser = webdriver.Chrome(service=webdriver_service, options=chrome_options) wait = WebDriverWait(browser, 10) url = 'https://portal.librus.pl/rodzina/synergia/loguj' browser.get(url) wait.until(EC.element_to_be_clickable((By.XPATH, '//a[@class = "btn btn-third btn-synergia-top btn-navbar dropdown-toggle"]'))).click() print('clicked to open dropdown') wait.until(EC.element_to_be_clickable((By.XPATH, '//a[@href="/rodzina/synergia/loguj"]'))).click() print('clicked to get the login interface') wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@id='caLoginIframe']"))) print('switched to iframe') wait.until(EC.element_to_be_clickable((By.XPATH, '//input[@id="Login"]'))).send_keys('My username') print('located and wrote into id field') wait.until(EC.element_to_be_clickable((By.XPATH, '//input[@id="Pass"]'))).send_keys('My password') print('located and wrote into password field') wait.until(EC.element_to_be_clickable((By.XPATH, '//button[@id="LoginBtn"]'))).click() print('logging in now...')
This will also print in terminal:
clicked to open dropdown clicked to get the login interface switched to iframe located and wrote into id field located and wrote into password field
Selenium setup is linux/chrome, you can adapt the code to your own setup, just observe the imports and code after defining the browser.
For Selenium documentation, please see https://www.selenium.dev/documentation/