【问题标题】:Selenium webdriver with python to scrape dynamic page cannot find elementSelenium webdriver与python抓取动态页面找不到元素
【发布时间】:2016-07-16 07:58:12
【问题描述】:

所以有很多关于stackoverflow上的动态内容抓取的问题,我经历了所有这些,但所有建议的解决方案都不适用于以下问题:

上下文:

问题:

我无法访问此页面上的任何 DOM 元素。请注意,如果我能得到一些关于如何访问搜索栏和搜索按钮的提示,那将是一个很好的开始。 See page to scrape 我最终想要的是浏览地址列表,启动搜索,然后复制屏幕右侧显示的信息。

我尝试了以下方法:

  • 更改了 webdriver 的浏览器(从 Chrome 到 Firefox)
  • 增加了页面加载的等待时间

    try:
        WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.ID, "addressInput")))
    except:
        print "address input not found"
    
  • 尝试通过 ID、XPATH、NAME、TAG NAME 等访问项目,但没有成功。

问题

  • 到目前为止我还没有尝试什么(使用 Selenium webdriver)?
  • 有些网站真的不能爬取吗? (我不认为这个城市在我每次重新加载页面时都使用算法来生成任何随机 DOM)。

【问题讨论】:

  • 使用 find_by_* 方法之一查找搜索字段,发送 Keys.ENTER
  • 问题是它找不到元素......不是关于如何发送密钥。
  • 你的问题有两部分:“如果我能得到一些关于如何访问搜索栏的提示,搜索按钮”...我提供了各种方法寻找 (find_by_*) 来定位一个元素。 (接受的答案选择find_element_by_id)。另请注意,在实践中按回车键绕过元素查找和模拟点击往往更快、更可靠。

标签: python selenium selenium-webdriver web-scraping scraper


【解决方案1】:

您可以使用此网址http://50.17.237.182/PIM/ 获取来源:

In [73]: from selenium import webdriver


In [74]: dr = webdriver.PhantomJS()

In [75]: dr.get("http://50.17.237.182/PIM/")

In [76]: print(dr.find_element_by_id("addressInput"))
<selenium.webdriver.remote.webelement.WebElement object at 0x7f4d21c80950>

如果你查看返回的源,有一个带有该 src url 的 frame 属性:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>
  <title>San Francisco Property Information Map </title>
  <META name="description" content="Public access to useful property information and resources at the click of a mouse"><META name="keywords" content="san francisco, property, information, map, public, zoning, preservation, projects, permits, complaints, appeals">
</head>
<frameset rows="100%,*" border="0">
  <frame src="http://50.17.237.182/PIM" frameborder="0" />
  <frame frameborder="0" noresize />
</frameset>

<!-- pageok -->
<!-- 02 -->
<!-- -->
</html>

感谢@Alecxe,它使用dr.switch_to.frame(0)的最简单方法:

In [77]: dr = webdriver.PhantomJS()

In [78]: dr.get("http://propertymap.sfplanning.org/")

In [79]:  dr.switch_to.frame(0)  

In [80]: print(dr.find_element_by_id("addressInput"))
<selenium.webdriver.remote.webelement.WebElement object at 0x7f4d21c80190>

如果您在浏览器中访问http://50.17.237.182/PIM/,您将看到与propertymap.sfplanning.org/ 完全相同的内容,唯一的区别是您可以完全访问使用前者的元素。

如果你想输入一个值并点击搜索框,它是这样的:

from selenium import webdriver


dr = webdriver.PhantomJS()
dr.get("http://propertymap.sfplanning.org/")

dr.switch_to.frame(0)

dr.find_element_by_id("addressInput").send_keys("whatever")
dr.find_element_by_xpath("//input[@title='Search button']").click()

但是如果你想提取数据,你可能会发现使用 url 查询是一个更简单的选择,你会从查询中得到一些 json。

【讨论】:

  • 我想你只需要切换到iframe:driver.switch_to.frame(0),对吧?
  • @alecxe,是的,这样会更容易:)
  • @PadraicCunningham:非常感谢,这太漂亮了——我生自己的气,因为我从没想过看那个。我已经花了整整 2 天的时间...
  • @AudreyBascoul,不客气,最终图像不是最好的,但如果您打开 firebig 或 chrome 开发工具并监控网络请求,您将更清楚地了解正在发生的事情。
  • @PadraicCunningham 谢谢!有时我使用提琴手。关于使用 url 发送查询的注意事项 :)
猜你喜欢
  • 2023-01-25
  • 2020-06-21
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多