3

I am scraping data from a webpage that is paginated, and once I finish scraping one page, I need to click the next button and continue scraping the next page. I then need to stop once I have scraped all of the pages and a next button no longer exists. Below contains the html around the "Next" button that I need to click.

<tr align="center"> 
   <td colspan="8" bgcolor="#FFFFFF">
     <br> 
     <span class="paging">
       <b> -- Page 1 of 3 -- </b>
     </span>
     <p>
       <span class="paging"> 
         <a href="page=100155&amp;by=state&amp;state=AL&amp;pagenum=2"> .          
           <b>Next -&gt;</b>
         </a> 
           &nbsp;&nbsp;
       </span> 
       <span class="paging"> 
         <a href=" page=100155&amp;by=state&amp;state=AL&amp;pagenum=3">Last -&gt;&gt;</a> 
       </span>
     </p>
   </td>
</tr>

I have tried selecting on class and on link text, and both have not worked for me in my current attempts.

2 examples of my code:

while True:
    try:
        link = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Next ->"))).click()
    except TimeoutException:
        break

while True:
        try:
            link = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "paging"))).click()
        except TimeoutException:
            break

All of the solutions I have found online have not worked, and have primarily ended with the following error:

ElementClickInterceptedException: Message: element click 
intercepted: Element <a href="? 
page=100155&amp;by=state&amp;state=AL&amp;pagenum=2">...</a> is not 
clickable at point (119, 840). Other element would receive the 
click: <body class="custom-background hfeed" style="position: 
relative; min-height: 100%; top: 0px;">...</body>
(Session info: chrome=76.0.3809.132)

If the remainder of the error code would be helpful to review, please let me know and I will update the post with this error.

I have looked at the following resources, all to no avail:

Python Selenium clicking next button until the end

python - How to click "next" in Selenium until it's no longer available?

Python Selenium Click Next Button

Python Selenium clicking next button until the end

Selenium clicking next button programmatically until the last page

How can I make Selenium click on the "Next" button until it is no longer possible?

Could anyone provide suggestions on how I can select the "Next" button (if it exists) and go to the next page with this set of HTML? Please let me know if you need any further clarification on the request.

CSlater
  • 63
  • 5
  • It looks like another element is receiving the click instead of Next element. i suggest you try to scroll down to the element and then try clicking on it. – Sureshmani Kalirajan Sep 10 '19 at 20:23
  • since it's on the body tag, that is probably a popup that is made to intercept the click. Try clicking the tag first. (These usually set a flag to allow on second click... and sometimes create a "popunder" by changing the current window/tab's location and opening the current page in the second window.) Post the markup and/or any client-side scripts. – pcalkins Sep 10 '19 at 21:07
  • Try clicking it with javascript. That will usually work. – pguardiario Sep 11 '19 at 00:48
  • Thank you all for your suggestions. Do you happen to have a possible solution in code that could help for this situation? I understand what you guys are saying, but I am relatively new to selenium and am not entirely sure the best way to implement what you're suggesting. – CSlater Sep 11 '19 at 15:18

1 Answers1

0

It sounds like you're asking two different questions here:

  1. How to click Next button until it no longer exists
  2. How to click Next button with Javascript.

Here's a solution to #2 -- Javascript clicking:

        public static void ExecuteJavaScriptClickButton(this IWebDriver driver, IWebElement element)  
        {
            ((IJavaScriptExecutor) driver).ExecuteScript("arguments[0].click();", element);
        }

In the above code, you have to cast your WebDriver instance as IJavascriptExecutor, which allows you to run JS code through Selenium. The parameter element is the element you wish to click -- in this case, the Next button.

Based on your code sample, your Javascript click may look something like this:

var nextButton = driver.findElement(By.LINK_TEXT, "Next ->"));
driver.ExecuteJavascriptClickButton(nextButton);

Now, moving onto your other issue -- clicking until the button is no longer visible. I would implement this in a while loop that breaks whenever the Next button no longer exists. I also recommend implementing a function that can check the presence of the Next button, and ignore the ElementNotFound or NoSuchElement exception in case the button does not exist, to avoid breaking your test. Here's a sample that includes an ElementExists implementation:


public bool ElementExists(this IWebDriver driver, By by)
{
    // attempt to find the element -- return true if we find it
    try 
    {
        return driver.findElements(by).Count > 0;
    }

    // catch exception where we did not find the element -- return false
    catch (Exception e)
    {
        return false;
    }
}

public void ClickNextUntilInvisible()
{
    while (driver.ElementExists(By.LINK_TEXT, "Next ->"))
    {

        // find next button inside while loop so it does not go stale
        var nextButton = driver.findElement(By.LINK_TEXT, "Next ->"));

        // click next button using javascript
        driver.ExecuteJavascriptClickButton(nextButton);
    }
}

This while loop checks for the presence of the Next button with each iteration. If the button does not exist, the loop breaks. Inside the loop, we call driver.findElement with each successive click, so that we do not get a StaleElementReferenceException.

Hope this helps.

CEH
  • 5,358
  • 2
  • 15
  • 36