-3

When I want to selenium click this code button , selenium write me this error

This is my code:

#LOGIN IN WEBSITE 
    browser = webdriver.Firefox()
    browser.get("http://class.apphafez.ir/")
    username_input = browser.find_element_by_css_selector("input[name='UserName']")
    password_input = browser.find_element_by_css_selector("input[name='Password']")
    username_input.send_keys(username_entry.get())
    password_input.send_keys(password_entry.get())
    button_go = browser.find_element_by_xpath("//button[@type='submit']")
    button_go.click()
    #GO CLASS
    wait = WebDriverWait(browser , 10)
    go_to_class = wait.until(EC.element_to_be_clickable((By.XPATH , ("//div[@class='btn btn-  palegreen enterClassBtn'"))))
    go_to_class.click()

this is site code :

<div class="databox-row   padding-10">
                    <button data-bind="attr: { 'data-weekscheduleId' :  Id}" style="width:100%" class="btn btn-palegreen enterClassBtn" data-weekscheduleid="320">"i want to ckick here"</button>

This is my program error :

  File "hafezlearn.py", line 33, in login_use
go_to_class = wait.until(EC.element_to_be_clickable((By.XPATH , ("//div[@class='btn btn-       palegreen enterClassBtn'")))) 
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
                </div>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • this is my error: File "hafezlearn.py", line 33, in login_use go_to_class = wait.until(EC.element_to_be_clickable((By.XPATH , ("//div[@class='btn btn-palegreen enterClassBtn'")))) File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/support/wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: – user13937766 Dec 05 '20 at 06:46

2 Answers2

1

You were close enough. The value of the class attribute is btn btn-palegreen enterClassBtn but not btn btn- palegreen enterClassBtn and you can't add extra spaces within the attribute value.


Solution

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

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-palegreen.enterClassBtn[data-bind*='data-weekscheduleId']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-palegreen enterClassBtn' and text()='i want to ckick here'][contains(@data-bind, 'data-weekscheduleId')]"))).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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Multiple class names for css values are tough to handle. usually easiest way is to use a css selector:

button.btn.btn-palegreen.enterClassBtn

Specifically:

go_to_class = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR , ("button.btn.btn-palegreen.enterClassBtn"))))

See also How to get elements with multiple classes

DMart
  • 2,401
  • 1
  • 14
  • 19