Selenium Webdriver:循环不工作

Selemium Webdriver: Loop not working

虽然运行代码

 def extract():
    driver = webdriver.Firefox()
    driver.get('http://example.com/')

    while True:
        elm = driver.find_element_by_link_text(">>").click()
        elm.click()

if __name__ == '__main__':
    extract()

页面加载并单击下一页按钮 (link) 并加载下一页, 新加载的页面也有相同的 link 按钮,但没有点击 并且,

出现错误:

Traceback (most recent call last):
  File "C:\Users\Admin\sel\click_next.py", line 14, in <module>
    extract_top_news()
  File "C:\Users\Admin\sel\click_next.py", line 11, in extract_top_news
    elm.click()
AttributeError: 'NoneType' object has no attribute 'click'

这是我试图点击的 link 按钮

<li>
<a href='http://example.com/page.php?page=2'>&gt;&gt;</a>
</li>

我错过了什么!

您确实喜欢双击,因为:

        elm = driver.find_element_by_link_text(">>").click()
        elm.click()

就像

driver.find_element_by_link_text(">>").click().click()

所以只需删除第一个 .click()