3

I am trying to write a program in Python that click to the next page until the it reaches to the last page. I followed some old posts on Stackoverflow and wrote the following code:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException


driver = webdriver.Chrome(executable_path="/Users/yasirmuhammad/Downloads/chromedriver")
driver.get("https://stackoverflow.com/users/37181/alex-gaynor?tab=tags")



while True:
    try:
        driver.find_element_by_link_text('next').click()

    except NoSuchElementException:
        break

However, when I run the program, it throws following error:

selenium.common.exceptions.WebDriverException: Message: unknown error: Element <a href="/users/37181/alex-gaynor?tab=tags&amp;sort=votes&amp;page=3" rel="next" title="go to page 3">...</a> is not clickable at point (1180, 566). Other element would receive the click: <html class="">...</html>
  (Session info: chrome=68.0.3440.106)

I also followed a thread of Stackoverflow (selenium exception: Element is not clickable at point) but no luck.

5 Answers 5

1

You need to close this banner first -

Since selenium opens a fresh browser instance so the website will ask you to store cookies every time you run the script. It is this exact banner which is coming in the way of selenium clicking your "next" button. Use this code to delete that close button -

driver.find_element_by_xpath("//a[@class='grid--cell fc-white js-notice-close']").click()

Also, driver.find_element_by_link_text('next') will throw a StaleElementReferenceException. Use this locator instead -

driver.find_element_by_xpath("//span[contains(text(),'next')]").click()

Final code -

driver.get("https://stackoverflow.com/users/37181/alex-gaynor?tab=tags")

driver.find_element_by_xpath("//a[@class='grid--cell fc-white js-notice-close']").click()


while True:
  try:
      time.sleep(3)
      driver.find_element_by_xpath("//span[contains(text(),'next')]").click()

    except NoSuchElementException:
        break
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer. But still getting the following error:
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
Thanks. Program works for couple of clicks but then it throws following error:
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document (Session info: chrome=68.0.3440.106)
@user2293224 My bad, check the final code, you need to put the sleep inside the loop so that it waits for some time when the new page loads. Its working on my side.
1

As per your question to click through the next page until the it reaches to the last page, you can use the following solution:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.common.exceptions import TimeoutException
    from selenium.common.exceptions import NoSuchElementException
    from selenium.common.exceptions import StaleElementReferenceException
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_argument('disable-infobars')
    driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://stackoverflow.com/users/37181/alex-gaynor?tab=tags")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='grid--cell fc-white js-notice-close' and @aria-label='notice-dismiss']"))).click()
    while True:
        try:
        driver.execute_script(("window.scrollTo(0, document.body.scrollHeight)"))
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='pager fr']//a[last()]/span[@class='page-numbers next']")))
        driver.find_element_by_xpath("//div[@class='pager fr']//a[last()]/span[@class='page-numbers next']").click()
        except (TimeoutException, NoSuchElementException, StaleElementReferenceException) :
        print("Last page reached")
        break
    driver.quit()
    
  • Console Output:

    Last page reached
    

Comments

0

There are couple of things that need to be taken care of:

  1. It seems the element is hidden by the cookies banner. By scrolling the page the element can be made available.
  2. When you click on the next - the page is reloaded. So you need to handle the StaleElementException.

Adding both these the code looks as follows:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import StaleElementReferenceException

driver = webdriver.Chrome()
driver.get("https://stackoverflow.com/users/37181/alex-gaynor?tab=tags")
driver.execute_script(("window.scrollTo(0, document.body.scrollHeight)"))

while True:
    try:
        webdriver.ActionChains(driver).move_to_element(driver.find_element_by_link_text('next')).click().perform()
    except NoSuchElementException:
        break
    except StaleElementReferenceException:
        pass

print "Reached the last page"
driver.quit()

Comments

0

I met the same error and the solution is not to scroll the window to the object(Maybe it can fix some errors but not in my case). My solution is using javascript, the code as follows:

click_goal = web.find_element_by_xpath('//*[@id="s_position_list"]/ul/li[1]/div[1]/div[1]/div[1]/a/h3')
web.execute_script("arguments[0].click();", click_goal)

Comments

0

problem:After login the page i cant able to click any element.when i try to click on the element it shows the error shild crossed.

My solution is using javascript,the code as follows: element = self.driver.find_element_by_xpath("/html/body/section[1]/div/ul/li[2]/ul/li/a self.driver.execute_script("arguments[0].click();",element)

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.