代码之家  ›  专栏  ›  技术社区  ›  Mario M.

通过xpath selenium phantomjs查找元素

  •  1
  • Mario M.  · 技术社区  · 7 年前

    Rselenium 用于报废。为此,我安装了 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
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   undetected Selenium    7 年前

    根据HTML,您可以使用 xpath 作为:

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

    :由于元素是动态生成的元素,您必须归纳 对于 元素是可见的 .

        2
  •  1
  •   hrbrmstr    7 年前

    你不需要一个重量级的第三方依赖。该站点使用graphql POST 异步XHR请求中的引擎盖下的请求,以检索数据。如果打开开发人员工具和 enter image description here

    我做了一个“复制POST数据”(在所有浏览器中通常都是相同或相似的上下文菜单项)并在Response选项卡中取消了graphql查询的最小化,只是为了向您显示它是什么,也许还为了,让您更容易查看查询并自行扩充查询(我刚才所说的超出了评论中“但是关于”后续问题的范围;如果需要帮助,请提出新问题)。

    '[
        {
            "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"
    

    邮递

    推荐文章