【问题标题】:find elements by xpath selenium phantomjs通过 xpath selenium phantomjs 查找元素
【发布时间】:2018-11-23 12:23:03
【问题描述】:

我正在使用 Rselenium 进行报废。为此,我在我的 Google Cloud 虚拟机实例中安装了java 和JDK's、chromedriver、selenium server standalone 和无头浏览器phantomjs。

我需要抓住第一条评分的文字:

remDr <- remoteDriver(browserName = 'chrome', port = 4444L)
remDr$open()
remDr$setWindowSize(1280L, 1024L)
remDr$navigate("https://www.ratebeer.com/reviews/sullerica-1561/294423")
text_post = remDr$findElements("xpath",'//*[@id="root"]/div/div[2]/div/div[2]/div[2]/div/div[1]/div[3]/div/div[2]/div[1]/div/div[2]/div/div[1]/div/div/div[1]')

text_post
## list()

最后text_post 是空的。

但是,如果我在本地笔记本电脑上使用 RSelenium、chrome 浏览器和相同的 XPath 测试相同的脚本,那就成功了!

发生了什么事?

是不是因为使用了phantomjs?

提前致谢。

sessionInfo()

R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.5 LTS

【问题讨论】:

    标签: r selenium selenium-webdriver phantomjs rselenium


    【解决方案1】:

    您不需要重量级的第三方依赖项。该站点在异步 XHR 请求中使用 graphql POST 请求来检索数据。打开开发者工具和就可以看到了

    我做了一个“复制 POST 数据”(在所有浏览器中通常是相同或非常相似的上下文菜单项),并在 Response 选项卡中取消最小化 graphql 查询,只是为了向您展示它是什么,也许还可以制作您可以更轻松地查看查询并自行对其进行扩充(我刚才所说的超出了“但是……”的范围,请关注 cmets 中的问题;如果您需要帮助,请提出一个新问题)。

    '[
        {
            "operationName": "beer",
            "query": "query beer($beerId: ID!) {\n  info: beer(id: $beerId) {\n    id\n    name\n    __typename\n  }\n}\n",
            "variables": {
                "beerId": "294423"
            }
        },
        {
            "operationName": "beer",
            "query": "query beer($beerId: ID!) {\n  info: beer(id: $beerId) {\n    id\n    name\n    styleScore\n    overallScore\n    averageRating\n    ratingCount\n    __typename\n  }\n}\n",
            "variables": {
                "beerId": "294423"
            }
        },
        {
            "operationName": "beerReviews",
            "query": "query beerReviews($beerId: ID!, $authorId: ID, $order: ReviewOrder, $after: ID) {\n  beerReviewsArr: beerReviews(beerId: $beerId, authorId: $authorId, order: $order, after: $after) {\n    items {\n      ...ReviewItem\n      __typename\n    }\n    totalCount\n    last\n    __typename\n  }\n}\n\nfragment ReviewItem on Review {\n  id\n  comment\n  score\n  scores {\n    appearance\n    aroma\n    flavor\n    mouthfeel\n    overall\n    __typename\n  }\n  author {\n    id\n    username\n    reviewCount\n    __typename\n  }\n  checkin {\n    id\n    place {\n      id\n      name\n      city\n      state {\n        id\n        name\n        __typename\n      }\n      country {\n        id\n        name\n        __typename\n      }\n      __typename\n    }\n    __typename\n  }\n  servedIn\n  likeCount\n  likedByMe\n  createdAt\n  updatedAt\n  __typename\n}\n",
            "variables": {
                "beerId": "294423",
                "first": 7,
                "order": "RECENT"
            }
        }
    ]' -> graphql_query
    

    我们需要将其压缩为 API 调用的一行(我使用下面的 gsub() 执行此操作。我们还需要手动指定内容类型并确保 httr 不会尝试破坏正文数据通过将编码设置为raw:

    httr::POST(
      url = "https://beta.ratebeer.com/v1/api/graphql/",
      httr::content_type("application/json"),
      encode = "raw",
      body = gsub("\n", " ", graphql_query),
      httr::verbose()
    ) -> res
    

    现在我们有一个结构化但高度嵌套的列表,其中包含您的 ifo。很确定你在关注下面的items:

    str(httr::content(res), 4)
    ## List of 3
    ##  $ :List of 1
    ##   ..$ data:List of 1
    ##   .. ..$ info:List of 3
    ##   .. .. ..$ id        : chr "294423"
    ##   .. .. ..$ name      : chr "Sullerica 1561"
    ##   .. .. ..$ __typename: chr "Beer"
    ##  $ :List of 1
    ##   ..$ data:List of 1
    ##   .. ..$ info:List of 7
    ##   .. .. ..$ id           : chr "294423"
    ##   .. .. ..$ name         : chr "Sullerica 1561"
    ##   .. .. ..$ styleScore   : num 35.1
    ##   .. .. ..$ overallScore : num 51.8
    ##   .. .. ..$ averageRating: num 3.25
    ##   .. .. ..$ ratingCount  : int 21
    ##   .. .. ..$ __typename   : chr "Beer"
    ##  $ :List of 1
    ##   ..$ data:List of 1
    ##   .. ..$ beerReviewsArr:List of 4
    ##   .. .. ..$ items     :List of 10
    ##   .. .. ..$ totalCount: int 21
    ##   .. .. ..$ last      : chr "7177326"
    ##   .. .. ..$ __typename: chr "ReviewList"
    

    它只有 21 个中的 10 个,因此在打开开发者工具的情况下在浏览器窗口中向下滚动并查看发出的第二个 POST 请求,查看更改了哪些参数,现在您将更好地了解如何访问网站的后端 API 而不是必须抓取内容。

    【讨论】:

      【解决方案2】:

      根据 HTML,您可以将 xpath 用作:

      //div[@id="root"]//span[contains(.,'20')]//following::div[contains(@class,'LinesEllipsis')]
      

      注意:由于元素是动态生成的元素,您必须诱导 WebDriverWait 以使 元素可见。

      【讨论】:

      • @MarioM。如果此/任何答案对您/对您有帮助,对未来的读者有帮助,请为答案投票。
      猜你喜欢
      • 2016-03-12
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      • 2022-07-05
      • 1970-01-01
      • 1970-01-01
      • 2021-10-11
      • 2020-11-29
      相关资源
      最近更新 更多