How to click on Search button using Python Selenium

Issue

I’m trying to click a search button with the help of selenium webdriver and python

Here is the HTML Code

<button data-testid="search-button" tabindex="4" type="submit" class="sc-2ry4jn-0       
sc-2ry4jn-2 sc-17kxwsy-0 bWjDpN" xpath="1"><div data-testid="icon-testid" class="sc- 
121424n-0 loEDwb"><div class="sc-121424n-2 jFTWvP"><span class="sc-1kvy6kt-0 jTNjLr sc- 
121424n-3 gCitZe" data-testid="icon:icon-jameda-SVG-icon-Search" color="#fff"><svg><use data-testid="svgcontainer-use" xmlns:xlink="http://www.w3.org/1999/xlink"  
xlink:href="#icon-jameda-SVG-icon-Search"></use></svg></span></div><div color="#fff" class="sc-121424n-1 hGbob">Suchen</div></div></button>
<div data-testid="icon-testid" class="sc-121424n-0 loEDwb" xpath="1"><div class="sc- 
121424n-2 jFTWvP"><span class="sc-1kvy6kt-0 jTNjLr sc-121424n-3 gCitZe" data- 
testid="icon:icon-jameda-SVG-icon-Search" color="#fff"><svg><use data- 
testid="svgcontainer-use" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon- 
jameda-SVG-icon-Search"></use></svg></span></div><div color="#fff" class="sc-121424n-1  
hGbob">Suchen</div></div>
<div class="sc-121424n-2 jFTWvP" xpath="1"><span class="sc-1kvy6kt-0 jTNjLr sc-121424n-3  
gCitZe" data-testid="icon:icon-jameda-SVG-icon-Search" color="#fff"><svg><use data- 
testid="svgcontainer-use" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon- 
jameda-SVG-icon-Search"></use></svg></span></div>
<span class="sc-1kvy6kt-0 jTNjLr sc-121424n-3 gCitZe" data-testid="icon:icon-jameda-SVG- 
icon-Search" color="#fff" xpath="1"><svg><use data-testid="svgcontainer-use"  
xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-jameda-SVG-icon-Search"> 
</use></svg></span>

To see the whole HTML Code visit:
www.jameda.de and check out the green search button in the right corner

I already tried to click it via CLASS_NAME, XPATH, LINK_TEXT but I always get the following error.

no such element: Unable to locate element:

Here is my code I used so far:

driver.find_element(by=By.CLASS_NAME, value="sc-2ry4jn-0 sc-2ry4jn-2 sc-17kxwsy-0 bWjDpN").click()

The button is visible when trying to click it.

Solution

To click on the element Suchen you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    driver.get('https://www.jameda.de/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span[class^='SearchString']"))).click()
    
  • Using XPATH:

    driver.get('https://www.jameda.de/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(., 'Suchen')]"))).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:

jameda

Answered By – undetected Selenium

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published