在Python中单击Selenium分页

2024-06-02 05:15:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python中的Selenium构建一个自动化框架,我尝试在其中循环访问HubSpot.com网站点击“下一步”按钮。在

根据Chrome Inspector,下一步按钮位于以下HTML中:

<a class="next" data-reactid=".0.1.0.0.2.1.0.0.1.0.$next.0">
    <span data-reactid=".0.1.0.0.2.1.0.0.1.0.$next.0.1">Next</span>
    <span class="right" data-reactid=".0.1.0.0.2.1.0.0.1.0.$next.0.2"></span>
</a>

我用下面的Python代码单击按钮:

^{pr2}$

其中,我的wait()函数定义为:

def wait(dr, x):
    element = WebDriverWait(dr, 5).until(
        EC.presence_of_element_located((By.XPATH, x))
    )
    return element

这在第一页上写得很好。但由于某种原因,当我进入第二页时,它无法点击按钮。没有错误,它只是在第三页开始做它应该做的,但是在第二页。另外,如果我在脚本应该点击第二页的Next按钮之前停止该脚本,我可以看到该按钮在Chromedriver中高亮显示。在

根据Chrome检查器,Next按钮位于第2页的以下HTML中:

<a class="next" data-reactid=".0.1.0.0.2.1.0.0.1.0.$next.0">
    <span data-reactid=".0.1.0.0.2.1.0.0.1.0.$next.0.1">Next</span>
    <span class="right" data-reactid=".0.1.0.0.2.1.0.0.1.0.$next.0.2"></span>
</a>

有什么建议吗?我没主意了。在


Tags: right脚本datahtmlseleniumelementchrome按钮
1条回答
网友
1楼 · 发布于 2024-06-02 05:15:47

我设法找到了一个解决办法。我没有尝试单击下一步按钮,而是单击相关的页面按钮。然后用I=2定义代码:

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

我仍然不明白为什么第一个解决方案不起作用,但这是一个有效的解决方案,点击分页。在

相关问题 更多 >