the star
the star

Reputation: 369

Click on a button search using classes with -selenium -python

I try clicking on the button search but this is not work. Here is the top code :

driver = webdriver.Chrome()
url = "https://www.flashscore.fr/"
driver.get(url)

the code that problem (first version):

buttonSearch = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".header__button--search")))
                 driver.execute_script("arguments[0].click();", buttonSearch)

This code does not what is asked but does not display an error.

So I have chosen a class in the class header__button--search named searchIcon___2C7g3ZD Here it is:

buttonSearch = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".searchIcon___2C7g3ZD")))
driver.execute_script("arguments[0].click();", buttonSearch)

It display this error : selenium.common.exceptions.JavascriptException: Message: javascript error: arguments[0].click is not a function

Why is it not possible of click on the button search from page ?

EDIT : here is code HTML of button search on the page :

<div id="search-window" class="header__button header__button--search"><span class="searchIcon___HyESXZA"><svg class="searchIcon___2C7g3ZD "><title>Search</title><use xlink:href="/res/_fs/build/control-icons-symbols.0acb5c0.svg#icon--search"></use></svg></span></div>

Upvotes: 1

Views: 141

Answers (1)

YJH16120
YJH16120

Reputation: 531


    buttonSearch = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".header__button--search"))) driver.execute_script("arguments[0].click();", buttonSearch) 

This does not work because you didn't call any method on buttonSearch. For example methods such as buttonSearch.click(), or buttonsearch.doubleClick(), etc. Without calling a method, the error will not be displayed. This is because instead of using the css selector of the element you instead used it's class name.

You then noticed that and amended that issue like so:


    buttonSearch = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".searchIcon___2C7g3ZD")))
    driver.execute_script("arguments[0].click();", buttonSearch)

However, that allowed the element to be located. But the webdriver is now unable to interact with the element. Because in the error it is explicitly stated that arguments[0].click is not a function. So to fix this, instead of of using execute_script simply use click() instead. It's a simpler way, and the intended way. Like this:


    buttonSearch = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".searchIcon___2C7g3ZD"))).click()

Upvotes: 1

Related Questions