Kim Megz Kim Megz - 1 year ago 116
Java Question

How to get a specific text from a javascript code in selenium webdriver by xpath

I want to get a specific line of text, the "URL" under the "clip:" of this code.

I tried


but it is getting the entire text. I only want to get the url. Is there any solution for this?.

<div id="player">
<script type='text/javascript'>
flowplayer("thisPlayer", {
src: "",
scaling: 'orig',
wmode: "opaque"
}, {
key: "some key",
clip: {
url: 'http://someurl/example.mp4',
scaling: 'fit',
autoPlay: false,
autoBuffering: true,
provider: 'lighttpd'

plugins: {
lighttpd: {
url: ""


Answer Source

I think you could not get the url from xpath.

After you have the text, as you did, you should use a regexp to get your url.

Maybe something like this:


This will save the url on matches.

Here an example on how to integrate in your code:

// From your example
var scriptText = driver.findElement(By.xpath("//*[@id="player"]/script[2]/text()")).getText();

// run the regexp on the script text
var matches = scriptText.match(/url:\s+"([^"]+)"/);

// check if you found something
if (matches.length > 0) {
    var url = matches[1];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download