Python 有没有办法让selenium单击此下一步按钮,直到它';使用类名而不是id不可访问

Python 有没有办法让selenium单击此下一步按钮,直到它';使用类名而不是id不可访问,python,html,selenium,Python,Html,Selenium,这是我要单击的按钮 <a class="paginate_button next" aria-controls="DataTables_Table_0" data-dt-idx="7" tabindex="0" id="DataTables_Table_0_next">Next</a> 目前,我正在这样做,dataTables显示第1页(共10页)、第2页(共10页)等等,所以我制作了一个python函数,当它变成第10页(共10页)时停止。这是html代码 <d

这是我要单击的按钮

<a class="paginate_button next" aria-controls="DataTables_Table_0" data-dt-idx="7" tabindex="0" id="DataTables_Table_0_next">Next</a>
目前,我正在这样做,dataTables显示第1页(共10页)、第2页(共10页)等等,所以我制作了一个python函数,当它变成第10页(共10页)时停止。这是html代码

<div class="dataTables_info" id="DataTables_Table_0_info" role="status" aria-live="polite">Page 1 of 10</div>
我的代码也能工作。但事情在下一页,它变成了DataTables\u Table\u 1和DataTables\u Table\u 1\u next

我试过这么做

driver.find_element_by_class_name('paginate_button next').click() 
但它似乎不起作用。它只是一次又一次地停留在同一页上


有人有办法解决这个问题吗?一种使用类名而不是id单击“下一步”按钮的方法,以便我可以将其应用于每个表。

您可以按如下方式获得“下一步”按钮的类。 然后,您可以使用while子句,每次单击元素后检查按钮的类。 如果类包含“disable”,则只需中断while

xpath = "//a[contains(@class,'paginate_button next')]"
while(True):
    # <- Put here the code snippet for getting the data for the page
    cls = driver.find_element_by_xpath(xpath).get_attribute("class")
    if 'disabled' not in cls:
        driver.find_element_by_xpath(xpath).click()
    else:
        break



xpath=“//a[contains(@class,'paginate_button next')”
虽然(正确):

#//*[@id=“DataTables\u Table\u 0\u next”]是XPATH,可以吗?在下一页上,它将是//*[@id=“DataTables\u Table\u 1\u next”]。这个id似乎在为我改变。使用class属性可以获得更健壮的解决方案。我正在编辑答案。您以前的代码运行正常,但现在我获得AttributeError:“WebDriver”对象没有属性“get_attribute”,出于某种原因,我正在尝试进行故障排除抱歉,出现错误。请用编辑过的答案再试一次。实际上,您需要检查内容是否已重新加载。如果页面重新加载,我们需要在单击元素后给予一些休息。
entries = driver.find_element_by_class_name('dataTables_info').text
entries = entries.replace(',', '')
check = [int(s) for s in entries.split() if s.isnumeric()]

if check[0]!=check[1]:
    driver.find_element_by_id('DataTables_Table_0_next').click()
driver.find_element_by_class_name('paginate_button next').click() 
xpath = "//a[contains(@class,'paginate_button next')]"
while(True):
    # <- Put here the code snippet for getting the data for the page
    cls = driver.find_element_by_xpath(xpath).get_attribute("class")
    if 'disabled' not in cls:
        driver.find_element_by_xpath(xpath).click()
    else:
        break