Ausgabe
Als ich mein Programm getestet habe, endet es mit Code 1
- hier ist ein vollständiger Fehler (
"Traceback (most recent call last): File "C:\Users\ds072\PycharmProjects\pythonProject\main.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"
)”) - Hier ist mein Code ( https://pastebin.com/RThnY8GY ) Ich habe Alternativen für diesen Wert ( ) getestet,
"/html/body/main/div/div/div/div[1]/div[3]/div[1]/div[1]/input"
aber ohne Ergebnisse: C
Bitte hilf mir
Lösung
Das Folgende ist eine Möglichkeit, diese Felder auszuwählen und sich bei dieser Website anzumelden (Felder befinden sich in einem 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...')
Dies wird auch im Terminal gedruckt:
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 ist Linux/Chrome, Sie können den Code an Ihr eigenes Setup anpassen, beobachten Sie einfach die Importe und den Code, nachdem Sie den Browser definiert haben.
Die Selenium-Dokumentation finden Sie unter https://www.selenium.dev/documentation/
Beantwortet von – Barry der Platipus
Antwort geprüft von – Senaida (FixError Volunteer)