pythonselenium-webdriverxpathwebdriverwaitexpected-condition

How to click on a button element using a Python bot


I want to click the send e-mail button in the code below, but I cannot access any fields such as selector, xpath ... and click the button. When you run the code, you will see that it already opens and closes the page. How can I provide a process here?

Code trials:

import time
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
from webdriver_manager.chrome import ChromeDriverManager

options = Options()
# options.add_argument("--headless")

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.delete_all_cookies()

driver.get(
    "https://suchen.mobile.de/fahrzeuge/details.html?id=355902358&damageUnrepaired=NO_DAMAGE_UNREPAIRED&isSearchRequest=true&pageNumber=1&scopeId=C&sortOption.sortBy=relevance&action=topOfPage&top=1:1&searchId=034dde38-e567-1e56-6d64-b8c8783a0e20&ref=srp")
time.sleep(10)
'''
email_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, 'E-mail schreiben')))
email_button.click()
'''
try:
    button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[contains(text(), 'E-Mail schreiben')]")))
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(), 'E-Mail schreiben')]"))).click()
except Exception as e:
    print("Error:", e)
    time.sleep(5)
driver.quit()

I tried to use:


Solution

  • To click on the element E-Mail schreiben instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

    E-Mail