1

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.

1
  • A simple xpath could be //span[text()=' Suchen'] Commented Jul 18, 2022 at 22:33

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

thx the CSS Selector worked. But i had to use it twice directly after another. Because first it just selects the search button, the second one is clicking it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.