Subha
Subha

Reputation: 1

Extracting the text value using getElementText - Rselenium

I am developing a automation tool for testing an GUI of an internal application. The automation tool is based on Rselenium. I am trying to assertion check for spellings, for this i want to extract text from html source.

I want to extract part1 in the html source code below.

Help will be much appreciated.

HTML source code:

<html class>
<head>
<body>
<div id="header">
<h1 style>part1</h1>
<h4 style>part2</h4>
</div>
</body>
</html> 

Rselenium code i used :

x <- remDr$findElement("id","header")
z <- x$getElementText()[[1]]
get("z")

Expected result ------> part1

Actual result --------> part1\npart2

Upvotes: 0

Views: 717

Answers (2)

Subha
Subha

Reputation: 1

x <- remDr$findElement("xpath",'//*[@id="part1"]/h1')
z <- x$getElementText()
get("z")

Gives you the expected result : part1

Upvotes: 0

niko
niko

Reputation: 5281

You could extract the html page using

wp <- remDr$getPageSource()

and then using rvest select the node in question

rvest::html_text(rvest::html_nodes(wp, 'h1'))

Here is a full example

# the html provided
html <- '<html class>
<head>
<body>
<div id="header">
<h1 style>part1</h1>
<h4 style>part2</h4>
</div>
</body>
</html> '

# read it as html
wp <- xml2::read_html(html)

# extract the data
rvest::html_text(rvest::html_nodes(wp, 'h1'))
# [1] "part1"

Upvotes: 1

Related Questions