1

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>
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
B.12
  • 21
  • 5

2 Answers2

0

You can't click on button, because you have consent modal that intercepting the click. So you need to accept modal and then click on the button

Like

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")

wdwait = WebDriverWait(driver, 10)
consent = wdwait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#mde-consent-modal-container [class*=mde-consent-accept-btn]")))
consent.click()

wdwait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR, "#mde-consent-modal-container")))

wdwait.until(EC.element_to_be_clickable((By.ID, "email-link-top"))).click()
Yaroslavm
  • 1,762
  • 2
  • 7
  • 15
0

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Yesss ! Thank you so much. – B.12 Aug 02 '23 at 14:27
  • Can I find out with which extension programme you found this xpath? Because I am trying to select the same number of other fields but it gives xpath error. I used applications such as Chropath, XpathFinder but it did not give any results. – B.12 Aug 03 '23 at 08:49
  • @B.12 Sorry, no _Chropath_ or _XpathFinder_, we write xpath/cssSelector on our own observing the DOM which eventually every automation should be able to do sooner or later. Take help of FireBug if needed. – undetected Selenium Aug 03 '23 at 09:57
  • 1
    okay, thank you so much. – B.12 Aug 03 '23 at 10:00