I am working on a web scraping project using Selenium in Python and am encountering an issue while interacting with the Lufthansa homepage. My goal is to click a specific element (the Departure field) after accepting cookies. While accepting the cookies works fine, attempting to click the Departure field results in a TimeoutException.
Here is the relevant part of my code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import time
driver = webdriver.Chrome()
driver.get("https://www.lufthansa.com/de/de/homepage")
time.sleep(2)
accept_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.ID, 'cm-acceptAll'))
)
accept_button.click()
time.sleep(5)
departure_field = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="dcep-tab-control-overlay4-checkIn-toggle"]/div/a/i[2]'))
)
departure_field.click()
time.sleep(5)
driver.quit()
I’ve also tried using JavaScript execution to click the Departure field, but this did not resolve the issue either:
departure_field = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="dcep-tab-control-overlay4-checkIn-toggle"]/div/a/i[2]'))
)
driver.execute_script("arguments[0].click();", departure_field)
The XPath seems correct as it highlights the intended element in the browser’s inspector. However, the problem persists.
Does anyone have any ideas on how to resolve this issue or what I might be overlooking? Any help or suggestions would be greatly appreciated!
Thank you in advance!
selenium4311 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.