3

I am trying to click on a button using selenium. My code states it is unable to find the css_selector with said class name. The class name has spaces in it, which lead me to use the css_selector object. When I try to pass the class name in the 'css_selector' object, it fails since the class name is a string, which is not callable. The website is password protected otherwise I would share the full code. This is what I have so far.

# Button I wish to click <a href="#" class="btn btn-alt see-full-list-btn">See Full List</a> # Once button is clicked, it changes to: <a href="#" class="btn btn-alt see-full-list-btn" style="display: none;">See Full List</a> 

What I have tried:

driver.find_element(By.CSS_SELECTOR("btn btn-alt see-full-list-btn")) # str object not callable driver.find_element(By.CSS_SELECTOR,"btn btn-alt see-full-list-btn") # Message: no such element: Unable to locate element: {"method":"css selector","selector":"btn btn-alt see-full-list-btn"} 

    2 Answers 2

    2

    As you've mentioned

    The class name has spaces in it, which lead me to use the css_selector

    this is right approach, however you should also make sure that one

    1. One should remove the space and put a .
    2. . represent class in CSS.

    So the below code should work:

    driver.find_element(By.CSS_SELECTOR, ".btn.btn-alt.see-full-list-btn") 

    or you can even use it with the tag a

    driver.find_element(By.CSS_SELECTOR, "a.btn.btn-alt.see-full-list-btn") 

    or the recommended solution would be to use with explicit waits:

    see_full_list_button = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.btn.btn-alt.see-full-list-btn"))) see_full_list_button.click() 

    Imports:

    from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC 
    2
    • 1
      Thank you for letting me know I am on the right path. However, that snippet still triggers the error: 'str' object is not callable.CommentedApr 5, 2022 at 18:03
    • @JackArmstrong: Got the issue it should have been this driver.find_element(By.CSS_SELECTOR, ".btn.btn-alt.see-full-list-btn")CommentedApr 5, 2022 at 18:06
    0

    There is no necessity to focus on the element HTML after the click is already invoked.

    As per the HTML

    <a href="#" class="btn btn-alt see-full-list-btn">See Full List</a> 

    you can use either of the following locator strategies:

    • Using link_text:

      driver.find_element(By.LINK_TEXT, "See Full List").click() 
    • Using css_selector:

      driver.find_element(By.CSS_SELECTOR, "a.btn.btn-alt.see-full-list-btn").click() 
    • Using xpath:

      driver.find_element(By.XPATH, "//a[@class='btn btn-alt see-full-list-btn' and text()='See Full List']").click() 

    Ideally to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

    • Using LINK_TEXT:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "See Full List"))).click() 
    • Using CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-alt.see-full-list-btn"))).click() 
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn btn-alt see-full-list-btn' and text()='See Full List']"))).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 

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.