0

I try to click on the next page link on this webpage: https://www.kumon.co.uk/find-a-tutor/ This page will be empty till you type a location like London, Manchester or wherever :)

The source-code looks like this:

<nav class="visible-xs-block">
             <ul class="pager">

                                        <li>
                        <a data-page="2" href="https://www.kumon.co.uk/find-a-tutor/?centre_search=london&page=2"><small><i class="fa fa-chevron-right"></i></small></a>
                    </li>

            </ul>
        </nav>

I try to make a click to the next page in 2 ways:

  • First trial:

    browser.find_elements_by_xpath("//*[@class='fa fa-chevron-right']/../..")[2].click()
    
  • Error:

    selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
    
  • Second trial:

    WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.XPATH, "//*[@class='fa fa-chevron-right']/../.."[2]))).click()
    
  • Error:

    selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.
    

What matters is that I can't find any documentation regarding the second error message, and that's why I post my first message on stackoverflow :)

FYI when I remove the click() the element is well localized so this is the click() which sucks seemingly :/

I have found out this solution dealing with Javascript which may be cool for me but I don't really know how to implement this => Selenium Element not visible exception

What do you think about it ?

Do you have any idea to help me please ? i'm stuck on this issue for 2 days unfortunately :(

EDIT:

This snippet:

while browser.find_element_by_xpath("//ul[@class='pagination']//li[last()]/a/small"):
        action = ActionChains(browser)
        search_button = WebDriverWait(browser, 20).until(
                EC.element_to_be_clickable((By.XPATH, "//ul[@class='pagination']//li[last()]/a/small")))
        action.move_to_element(search_button)
        sleep(1)
        search_button.click()

raises an error selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

And I'm not sure that using try...except is the best solution to solve it, is it ?

Thank's in advance, Nicolas.

2 Answers2

1

To invoke click() on the > icon to move to the next page you need to induce WebDriverWait for the desired element to be clickable and you can use the following solution:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='pagination']//li[last()]/a/small"))).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

Looks xpath which used is not correct. Its good to cross check xpath in browser console. xpath provided in above answer @DebanjanB is good. try once: in chrome->F12->Console: paste below one and Enter.

  $x("//ul[@class='pagination']//li[last()]/a/small") 

It has to fetch required element. Similarly try all xpaths which you used in posted question, they are fetching wrong elements which leads to exceptions on execution.

murali selenium
  • 3,847
  • 2
  • 11
  • 20