简体   繁体   English

麻烦点击下一页的按钮

[英]Trouble clicking on the button for the next page

I've written some code in python in combination with selenium. 我已经在python中结合硒编写了一些代码。 I intended to parse the table from a webpage. 我打算从网页解析表。 I've got it working. 我已经工作了。 However, trouble comes up when i try to click on the next page button. 但是,当我尝试单击下一页按钮时会遇到麻烦。 The scraper only parse the table from the first page and instead of clicking the next button it quits without throwing any error. 刮板仅从第一页分析表,而不单击下一个按钮,它退出而不会引发任何错误。 So, i can't understand what I'm missing. 所以,我不明白我所缺少的。

Here is the full code for your consideration: 这是完整的代码供您考虑:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)

driver.get("https://toolkit.financialexpress.net/santanderam")

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'table.fe-datatable')))

tab_data = driver.find_element_by_css_selector('table.fe-datatable')

while True:
    wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'tr')))
    list_rows = [[cell.text for cell in row.find_elements_by_css_selector('td')]
                 for row in tab_data.find_elements_by_css_selector('tr')]
    for data in list_rows:
        print(data)

    try:
        driver.find_element_by_css_selector('a.ui-paging-next').click()
    except:
        break

driver.quit()

Elements within which the next-page button exists: 下一页按钮所在的元素:

<div class="pagination ui-widget"><span class="ui-paging-current ui-state-default ui-state-disabled ui-corner-all ui-paging-prev">Prev</span><span class="ui-paging-current ui-state-default ui-state-disabled ui-state-highlight ui-corner-all">1</span><a class="ui-paging-button ui-state-default ui-corner-all" href="#">2</a><a class="ui-paging-button ui-state-default ui-corner-all" href="#">3</a><a class="ui-paging-button ui-state-default ui-corner-all" href="#">4</a><span class="ui-state-default ui-corner-all ui-state-disabled ui-paging-ellipse">...</span><a class="ui-paging-button ui-state-default ui-corner-all ep" href="#">7</a><a class="ui-paging-button ui-state-default ui-corner-all ui-paging-next" href="#">Next</a></div>

@Grasshopper has already provided with a solution, but I'll try to give more details for you to understand why your code fails @Grasshopper已经提供了解决方案,但是我将尝试提供更多详细信息,以帮助您理解代码为何失败

There are two links with the same HTML code present in page source: the first is hidden, second (the one that you need) is not. 页面源中存在两个具有相同HTML代码的链接:第一个链接是隐藏的,第二个(您需要的链接)不是。

You can check it with 你可以用

print(len(driver.find_elements_by_css_selector('a.ui-paging-next')))

While css-selector or XPath returns you simply the first occurence, search by link text returns link with the visible text only: 尽管css-selector或XPath仅是第一次出现,但是按链接文本搜索仅返回带有可见文本的链接:

print(len(driver.find_elements_by_link_text('Next')))

That's why your find_element_by_css_selector(...) code doesn't work, but find_element_by_link_text(...) does. 这就是为什么您的find_element_by_css_selector(...)代码不起作用,但是find_element_by_link_text(...)起作用的原因。

Also note that line 另请注意

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'table.fe-datatable')))

should already return you required element, so there is no need in 应该已经返回了您必需的元素,所以不需要

tab_data = driver.find_element_by_css_selector('table.fe-datatable')

Just use 只需使用

tab_data = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'table.fe-datatable')))

To avoid getting StaleElementReferenceException you should re-define your tab_data on each iterarion as tab_data defined on first page will not be accessible on the next page. 为了避免让StaleElementReferenceException你应该重新定义你的tab_data每个iterarion作为tab_data第一页上定义不会成为下一个页面上访问。 Just put tab_data definition inside the while loop 只需将tab_data定义放入while循环中

UPDATE UPDATE

In your code try to replace 在您的代码中尝试替换

try:
    driver.find_element_by_link_text('Next').click()
except:
    break

with

first_row = driver.find_element_by_css_selector('table.fe-datatable tr.odd').text
try:
    driver.find_element_by_link_text('Next').click()
except:
    break
wait.until(lambda driver: driver.find_element_by_css_selector('table.fe-datatable tr.odd').text != first_row)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM