gpt4 book ai didi

ruby-on-rails - Watir-webdriver 没有抓取指定的 div,不确定为什么?

转载 作者:太空宇宙 更新时间:2023-11-03 17:43:50 25 4
gpt4 key购买 nike

这是 Controller 的代码,我正在尝试使用。

class ScraperController < ApplicationController

def getinformation
require 'watir-webdriver'
require 'phantomjs'

@browser = Watir::Browser.new:phantomjs
@browser.goto "http://reddit.com"
@divs = @browser.divs(class: "title may-blank ")

end

end

在 View 页面上,我有 <%= @divs.length %> 并且它正在输出 0。当我在 View 页面上键入 <%= browser.title %> 时,它会显示“reddit:首页internet”,所以我知道我从网页上得到了一些东西。

我通过检查主页上一些帖子的标题找到了 div 类“title may-blank”,并且我尝试使用 div 类“title may-blank”和“title may-blank”。一个在末尾有空格,当它在 reddit 页面上被检查时是如何显示的,另一个在末尾没有空格。两者都没有给我任何东西。

最佳答案

假设您正在尝试定位:

<a class="title may-blank outbound" data-event-action="title" href="https://i.redd.it/kxtpiskh9mez.jpg" tabindex="1" data-href-url="https://i.redd.it/kxtpiskh9mez.jpg" data-outbound-url="https://out.reddit.com/t3_6si8wj?url=https%3A%2F%2Fi.redd.it%2Fkxtpiskh9mez.jpg&amp;token=AQAAKQiLWXNK30ZkH9-TplVYPCVEIJwNWgqFZCdFuOc3O5l1c7M1&amp;app_name=reddit.com" data-outbound-expiration="1502283817000" rel="">Fries</a>

问题是元素是 a 而不是 div。你至少需要做:

@browser.links(class: "title may-blank ")

虽然这会返回结果,但它可能不是您所期望的一切。在 String 中支持多个类也在 Watir 中被弃用。

我想你实际上只是想要:

@browser.links(class: "title")

如果您确实需要这两个类,请使用:

@browser.links(class: ["title", "may-blank"])

关于ruby-on-rails - Watir-webdriver 没有抓取指定的 div,不确定为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45588826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com