Click through pagination with Selenium in Python
<p>I'm working on building an automation framework using Selenium in Python where I am trying to loop through landing pages on HubSpot.com by clicking on the "Next" button.</p>

<p>The Next button is located in the following HTML according to the Chrome Inspector:</p>

<pre><code>&lt;a class="next" data-reactid=".0.1.0.0.2.1.0.0.1.0.$next.0"&gt;
    &lt;span data-reactid=".0.1.0.0.2.1.0.0.1.0.$next.0.1"&gt;Next&lt;/span&gt;
    &lt;span class="right" data-reactid=".0.1.0.0.2.1.0.0.1.0.$next.0.2"&gt;&lt;/span&gt;
&lt;/a&gt;
</code></pre>

<p>And I'm clicking the button using the following Python code:</p>

<pre><code>time.sleep(3)
wait(self.driver, """//a[@class="next" and not(@disabled)]""")
            nextButton = self.driver.find_element(By.XPATH, """//a[@class="next" and not(@disabled)]""")
            hov = ActionChains(self.driver).move_to_element(nextButton)
            hov.click().perform()
</code></pre>

<p>where my wait() function is defined as:</p>

<pre><code>def wait(dr, x):
    element = WebDriverWait(dr, 5).until(
        EC.presence_of_element_located((By.XPATH, x))
    )
    return element
</code></pre>

<p>This works perfectly fine on the first page. But for some reason when I get to page two, it's not able to click the button. No errors, it just starts doing what it should on page three - but on page two. Also, if I stop the script just before it's supposed to click on the Next button on page two, I can see that the button is highlighted in the Chromedriver.</p>

<p>The Next button is located in the following HTML on page 2 according to the Chrome Inspector::</p>

<pre><code>&lt;a class="next" data-reactid=".0.1.0.0.2.1.0.0.1.0.$next.0"&gt;
    &lt;span data-reactid=".0.1.0.0.2.1.0.0.1.0.$next.0.1"&gt;Next&lt;/span&gt;
    &lt;span class="right" data-reactid=".0.1.0.0.2.1.0.0.1.0.$next.0.2"&gt;&lt;/span&gt;
&lt;/a&gt;
</code></pre>

<p>Any suggestions? I am out of ideas.</p>

<p>I have managed to find a workaround. Instead of trying to click the next button I click the relevant page-button. I define j=2 to begin with, and then use the following code:</p>

<pre><code>if self.driver.find_elements(By.XPATH, """//a[@class="next" and not(@disabled)]""") != 0:
                xpathstring = """//div[@class="hs-pagination"]/ul/li/a[text()="%s"]""" % (j)
                waitclick(self.driver, xpathstring)
                nextButton = self.driver.find_element(By.XPATH, xpathstring)
                hov = ActionChains(self.driver).move_to_element(nextButton)
                hov.click().perform()
                time.sleep(3)
                j = j + 1
</code></pre>

<p>I still do not understand why the first solution does not work, but this is a working solution to clicking through pagination.</p>

python selenium onclick click