Finding element anchor class element with selenium python causing css selector error


With the HTML:

<a class="paginate_button next" aria-controls="tabcc" data-dt-idx="7" tabindex="0" id="tabcc_next">Next</a>

I am trying to grab this by class to select the ‘Next’ innerHTML. I am trying:

next_page = self.driver.find_element_by_class_name('paginate_button next')


next_page = WebDriverWait(self.driver, 20).until(
   EC.presence_of_element_located((By.CLASS_NAME, "paginate_button next"))

but both give the error:

 raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".paginate_button next"}
  (Session info: chrome=91.0.4472.114)

Doing the same idea with the ID seems to be working:

next_page = self.driver.find_element_by_id('tabcc_next')

However, I need it to work for the class name for what I am doing specifically.

Any help would be appreciated!


You are trying to locate element according its’ PARTIAL class attribute while locating element with find_element_by_class_name requires the EXACT class attribute value.
Selecting element by partial attribute value possible with css_selectors or XPath.
So you can use css_selector instead.

next_page = self.driver.find_element_by_css_selector('')


next_page = WebDriverWait(self.driver, 20).until(
   EC.presence_of_element_located((By.CSS_SELECTOR, ""))

or XPath

next_page = self.driver.find_element_by_xpath("//a[contains(@class,'paginate_button next')]")


next_page = WebDriverWait(self.driver, 20).until(
   EC.presence_of_element_located((By.XPATH, "//a[contains(@class,'paginate_button next')]"))

Answered By – Prophet

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published