【问题标题】:When I click a button that leads to another page, how do I get the contents of the new page?当我单击一个指向另一个页面的按钮时,如何获取新页面的内容?
【发布时间】:2015-11-19 15:13:36
【问题描述】:

我正在使用selenium-webdriver 来抓取网站。当浏览器单击“下一步”按钮时,会加载下一页,但是当我尝试查找所需的元素时,驱动程序会打印上一页的内容。

这是我的脚本:

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
url = 'http://www.airforwarders.org/companies'
page = driver.navigate.to(url)

driver.find_elements(:css=>'.item_main').each{|div|
    puts div.text 
}

paginationToolbar = driver.find_element(:css=>'.pagination-toolbar')
paginationToolbar.find_elements(:css=>'.btn')[-2].click # Clicking the "next" button
driver.find_elements(:css=>'.item_main').each{|div|
    puts div.text # This shows the same stuff from the previous loop
}

如果我可以从新页面中获取内容,那就没问题了。我该怎么做?

【问题讨论】:

    标签: ruby selenium selenium-webdriver web-scraping


    【解决方案1】:

    如果您确定 Selenium 已经点击了加载下一页的按钮,那么我认为您应该在点击后添加 sleep 1

    【讨论】:

    • 请不要这样做sleeps...正确的方法是等待新页面上的元素可以访问。这样一来,您的脚本不会在 1 秒不够时中断,而在您不需要一秒时运行得更快。
    【解决方案2】:

    可能在单击操作后的那一刻 ajax 没有完成。 尝试等待 1-3 秒,然后再执行其他操作。

    【讨论】:

      猜你喜欢
      • 2021-08-19
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多