Search code examples
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:

  • Full Xpath:

    /html/body/div[5]/div/div[2]/div[3]/div[2]/aside/div[1]/div/div[4]/div[2]/div/div/span 
    
  • Selector:

    #email-link-top > span 
    
  • Xpath:

    //*[@id="email-link-top"]/span
    
  • Element HTML:

    <span class="btn btn--primary btn--l u-full-width"><i class="gicon-email-white-s icon--s"></i>E-Mail schreiben</span>
    

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:

    • Code block:

      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")
      # accept the cookies
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.mde-consent-accept-btn"))).click()
      # click on E-Mail schreiben
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='email-link-top-container']//span[contains(text(), 'E-Mail schreiben')]"))).click()
      
    • Note: You have to add the following imports :

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
    • Browser snapshot:

    E-Mail