1

I'm using the following code to identify the Next button, but Selenium does not find it

driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))
driver.implicitly_wait(10)
driver.get(url)

python_button = driver.find_element(By.CLASS_NAME, "nav next rndBtn ui_button primary taLnk")
python_button.click()

The error is the following:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .nav next rndBtn ui_button primary taLnk

Snapshot of the HTML:

enter image description here

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
peetman
  • 669
  • 2
  • 15
  • 30

1 Answers1

1

To locate the clickable element further to invoke click on it 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, "a.nav.next.rndBtn.ui_button.primary.taLnk[data-page-number][onclick*='STANDARD_PAGINATION']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='nav next rndBtn ui_button primary taLnk'][@data-page-number and contains(@onclick, 'STANDARD_PAGINATION')]"))).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
    

References

You can find a couple of relevant discussions on NoSuchElementException in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • it does not work. I does not find the element again. please see my updated post, I have added the waittime as well. – peetman Mar 16 '23 at 21:14
  • _I have added the waittime as well_: Adding _`implicitly_wait()`_ may not be of any help. Just copy paste the line of code from the answer and update me the result. – undetected Selenium Mar 16 '23 at 21:18
  • so if I use XPATH it does not find the element. if I use CSS_SELECTOR it does not give any error, but it does not click the actual button on the webpage. – peetman Mar 16 '23 at 21:37
  • Did you happen to check the linked reference discussions, incase your usecase isn't generic and needs some more tweaks? – undetected Selenium Mar 16 '23 at 21:39
  • Thanks. Now it works to click on the button, but I cannot get the new page_source. The link in Tripadvisor does not update btw. After `click()` I run again `s_city = BeautifulSoup(driver.page_source, "lxml")` – peetman Mar 16 '23 at 22:16
  • Glad to be able to help you. _"I cannot get the new page_source"_ sounds to be a different question all together. Feel free to raise a new question with your new requirements. – undetected Selenium Mar 16 '23 at 22:18