Skip to content

rvest xpath must start with a single quote followed by double quoting any internal strings else does not work #435

Open
@SugarRayLua

Description

@SugarRayLua

CCing issue above issue discovered from posting to stack overflow:

https://stackoverflow.com/questions/79267016/how-to-properly-perform-an-xpath-english-text-search-with-r-package-rvest-doesn/79268974#79268974

library(rvest)
library(dplyr)

test <- read_html_live("https://www.cdc.gov")

# single-quotes in xpath, no match:
html_elements(test, xpath = "//a[contains(text(),'Outbreaks')]")
#> {xml_nodeset (0)}

# swap quotes, " <-> '
html_elements(test, xpath = '//a[contains(text(), "Outbreaks")]')
#> {xml_nodeset (3)}
#> [1] <a href="https://www.cdc.gov/outbreaks/index.html">Outbreaks</a>
#> [2] <a href="https://www.cdc.gov/outbreaks/index.html" class="btn btn-outline ...
#> [3] <a href="https://www.cdc.gov/outbreaks/index.html" class="btn btn-outline ...

Perhaps a note about the needed single quotation starting order to an xpath could be added to the rvest documentation
Fyi

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions