0

I searched stack overflow and tried some solutions but could not make it work.

Here is my code:

from selenium import webdriver

PATH = "C:\Program Files (x86)\chromedriver.exe";
driver = webdriver.Chrome(PATH)

driver.get("https://coinmarketcap.com/");
driver.implicitly_wait(5)
tt = driver.find_element_by_class_name('zafg3t-1')
print(tt)

driver.quit()

The class name zafg3t-1 is the search area class, I can see in browser, but selenium cant find the class. So I unable to click.

What is my mistake here?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

Classnames like zafg3t-1, gaWePq, etc are dynamic generated and would change after a considerable amount of time and even may be the next time you access the site afresh.


Solution

To click within the Search box 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://coinmarketcap.com/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.cmc-cookie-policy-banner__close"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[data-text='Use to trigger search']"))).click()
    
  • Using XPATH:

    driver.get('https://coinmarketcap.com/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='cmc-cookie-policy-banner__close']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Search']"))).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:

coinmarketcap

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hi, thanks for reply. But the code you have provided can not grab the element. Here is the matter. Dont know why, it can not grab the element – Imran Ahmed Jul 07 '22 at 06:58
  • What do mean by _grab the element_? Can you just copy/paste the lines of code from this answer and retest? – undetected Selenium Jul 07 '22 at 08:59
  • Both in css_selector and xpath `cmc-cookie-policy-banner__close` line, these are not showing error, but not clicking anything too. And in both css_selector and xpath "Search" option line, triggering timeout error. `raise TimeoutException(message, screen, stacktrace)` – Imran Ahmed Jul 07 '22 at 10:18
  • Here is full error: WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Search']"))).click() File "F:\python\installed2\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: – Imran Ahmed Jul 07 '22 at 10:19