0

I trying to click on the attribute class container in the tag div with a librarie Selenium. Here is code :

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get('https://www.flashscore.com/')

driver.find_element(By.CSS_SELECTOR, ".header__button header__button--search").click()

And here is error display :

>>> driver.find_element(By.CSS_SELECTOR, ".header__button header__button--search").click();
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\avis\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py",

line 976, in find_element return self.execute(Command.FIND_ELEMENT, { File "C:\Users\avis\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "C:\Users\avis\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".header__button header__button--search"} (Session info: chrome=85.0.4183.102)

the code is inspired of the doc of selenium : https://www.selenium.dev/documentation/en/getting_started_with_webdriver/performing_actions_on_the_aut/

I did some research and find a function which makes it possible to overcome the exception and it'is :element_to_be_clickable() it is used to wait as the element is display and be clickable, according to the doc. And I used it as this :

> > element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "header__button
> header__button--search"))
>     element.click();

But this syntaxe error is display in console :

File "", line 2 element.click() ^ SyntaxError: invalid syntax

while I not see of syntax error. From where can arise from the error ? And function use is good ?

mouse
  • 5
  • 1
  • 4
  • 2
    It looks like the data is loaded dynamically through javascript. You will probably need an automation tool like Selenium. – Mike67 Sep 15 '20 at 02:56
  • Thanks, I'm changed of librarie and therefore also changed the subject but I'm other problem. Could you help me ? – mouse Sep 16 '20 at 23:34

1 Answers1

0

In HTML, you specify multiple classes with a space:

<span class="class1 class2">....</span>

In selenium, when searching for a single class, just use one of the class names:

driver.find_element(By.CSS_SELECTOR, ".class1")

This will code will open the site with Selenium and click the Search button:

from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver

# prevent version errors and plugin warning, may not be needed for you
options = webdriver.ChromeOptions()
options.add_argument("disable-extensions")
options.add_argument("disable-plugins")
options.experimental_options["useAutomationExtension"] = False  # prevent load error - Error Loading Extension - Failed to load extension from ... - Could not load extension from ... Loading of unpacked extensions is disabled
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)

# main code
driver.get('https://www.flashscore.com/')

element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".header__button--search")))

driver.find_element(By.CSS_SELECTOR, ".header__button--search").click()

To find an element using multiple classes, check this post: Find div element by multiple class names?

Mike67
  • 11,175
  • 2
  • 7
  • 15
  • Thanks, for help you, but I believe that the midlle lines are for install chrome driver except that I' ve already chrome driver. I'm keeped my basic code then just changed last line and replace with your last line and it worked. But why the class is find while it is not in full ? selenium not accept the space ? – mouse Sep 17 '20 at 10:58
  • That element has two classes. Answer updated with explanation. – Mike67 Sep 17 '20 at 14:13