【问题标题】:ruby selenium web driver - get google knowledge graph contentruby selenium web driver - 获取谷歌知识图内容
【发布时间】:2015-09-01 12:56:04
【问题描述】:

我正在使用 ruby​​ selenium 网络驱动程序并尝试从<div class="xpdopen"> 中的第一个谷歌搜索结果页面上的搜索结果中获取位于右上角的谷歌知识图的内容

@driver = Selenium::WebDriver.for :phantomjs
@driver.manage.timeouts.implicit_wait = 10
@driver.get "http://google.com"
element = @driver.find_element :name => "q"
element.send_keys "BMW"
element.submit
content = @driver.find_element(:class, 'xpdopen')

但是selenium找不到这个元素会报错

#<Selenium::WebDriver::Error::NoSuchElementError: {"errorMessage":"Unable to find element with class name 'xpdopen'"

当我在 chrome js 控制台$('.xpdopen') 中尝试时,它立即找到了这个元素

我也试过

@driver.execute_script("return document.getElementsByClassName('xpdopen');")

但是找不到这个元素

我还在页面源中绑定了@driver.page_source 和&lt;div class="xpdopen"&gt;,但我可以在 chrome 控制台中看到它。为什么?

我怎样才能用硒获得这个元素?

这是我从 pry 中得到的结果:

[21] pry(main)> @driver = Selenium::WebDriver.for :phantomjs
=> #<Selenium::WebDriver::Driver:0x..f822d288ec7f0a708 browser=:phantomjs>
[22] pry(main)> @driver.manage.timeouts.implicit_wait = 10    
=> 10
[23] pry(main)> @driver.get "http://google.com"    
=> {}
[24] pry(main)> element = @driver.find_element :name => "q"    
=> #<Selenium::WebDriver::Element:0x..f389f4a8876f601e id=":wdc:1434526425103">
[25] pry(main)> element.send_keys "BMW"    
=> nil
[26] pry(main)> element.submit    
=> {}
[27] pry(main)> sleep 10    
=> 10
[28] pry(main)> content = @driver.find_element(:xpath, '//*[@id="rhs_block"]/ol/li/div[1]/div')    
Selenium::WebDriver::Error::NoSuchElementError: {"errorMessage":"Unable to find element with xpath '//*[@id=\"rhs_block\"]/ol/li/div[1]/div'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"gzip;q=1.0,deflate;q=0.6,identity;q=0.3","Connection":"close","Content-Length":"67","Content-Type":"application/json; charset=utf-8","Host":"127.0.0.1:8929","User-Agent":"Ruby"},"httpVersion":"1.1","method":"POST","post":"{\"using\":\"xpath\",\"value\":\"//*[@id=\\\"rhs_block\\\"]/ol/li/div[1]/div\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/2f3cf350-14c3-11e5-9f8e-4173e8049986/element"}} (org.openqa.selenium.NoSuchElementException)

和

[29] pry(main)> content = @driver.find_element(:css, "#rhs_block > ol > li > div.kp-blk._Jw._Rqb._RJe > .xpdopen")
Selenium::WebDriver::Error::NoSuchElementError: {"errorMessage":"Unable to find element with css selector '#rhs_block > ol > li > div.kp-blk._Jw._Rqb._RJe > .xpdopen'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"gzip;q=1.0,deflate;q=0.6,identity;q=0.3","Connection":"close","Content-Length":"113","Content-Type":"application/json; charset=utf-8","Host":"127.0.0.1:8929","User-Agent":"Ruby"},"httpVersion":"1.1","method":"POST","post":"{\"using\":\"css selector\",\"value\":\"#rhs_block \\u003e ol \\u003e li \\u003e div.kp-blk._Jw._Rqb._RJe \\u003e .xpdopen\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/2f3cf350-14c3-11e5-9f8e-4173e8049986/element"}} (org.openqa.selenium.NoSuchElementException)

只是为了证明它可以在同一页面上毫无问题地找到其他元素:

[30] pry(main)> results = @driver.find_elements(:xpath, "//p/a") 
=> [#<Selenium::WebDriver::Element:0x6f6a74631e2b7010 id=":wdc:1434527087873">,
 #<Selenium::WebDriver::Element:0x7b6d276448081688 id=":wdc:1434527087874">,
 #<Selenium::WebDriver::Element:0x..f9504a4171b03970a id=":wdc:1434527087875">,
 #<Selenium::WebDriver::Element:0x..fa6e0158aa8d24e2a id=":wdc:1434527087876">,
 #<Selenium::WebDriver::Element:0x327bf842e4399368 id=":wdc:1434527087877">,
 #<Selenium::WebDriver::Element:0x..fae292d7ca211ab32 id=":wdc:1434527087878">,
 #<Selenium::WebDriver::Element:0x129a58eb5ed6ee9c id=":wdc:1434527087879">,
 #<Selenium::WebDriver::Element:0x46ef3b45800e63e0 id=":wdc:1434527087880">,
 #<Selenium::WebDriver::Element:0x26bfb47f8ad498ea id=":wdc:1434527087881">,
 #<Selenium::WebDriver::Element:0x..f03756c2924a2974 id=":wdc:1434527087882">,
 #<Selenium::WebDriver::Element:0xfba93aab4b32af8 id=":wdc:1434527087883">]

我用截图发现phantomjs没有显示(没有内容)知识图谱

phantomjs 的截图

来自 Firefox 的屏幕截图

为什么phantomjs没有内容知识图谱?

【问题讨论】:

    标签: javascript css ruby selenium web-scraping


    【解决方案1】:

    显然 css 自己不知道在哪里可以找到 xpdopen 类,你必须给出元素的整个路径:

    Xpath:

    content = @driver.find_element(:xpath, "//*[@id="rhs_block"]/ol/li/div[1]/div")
    

    CSS:

    content = @driver.find_element(:css, "#rhs_block > ol > li > div.kp-blk._Jw._Rqb._RJe > .xpdopen")
    

    【讨论】:

    • 感谢您的帮助。我尝试了你的解决方案,它给出了同样的错误并且没有为 xpdopen 元素提供资金。
    • 我已经尝试过了,它对我有用...你能确保在尝试映射元素之前等待搜索完成吗?或者更好的是,在失败的步骤之前使用 binding.pry,确保所有内容都已加载并尝试在命令行中手动映射元素......让我们知道结果
    • 我刚刚编辑了从 pry 到我的问题的结果
    • 这里找不到问题所在
    • 在这种情况下无法找到正确的路径,因为 phantomjs 没有内容“知识图”,我只是添加了 firefox 和 phantomjs 的屏幕截图,我们可以在屏幕截图中看到它缺失phantomjs。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 2012-09-23
    • 2022-11-29
    • 1970-01-01
    相关资源
    最近更新 更多