Kim Megz Kim Megz - 1 month ago 14
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

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


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: "http://example.com.swf",
scaling: 'orig',
wmode: "opaque"
}, {
key: "some key",
clip: {
url: 'http://someurl/example.mp4',
scaling: 'fit',
autoPlay: false,
autoBuffering: true,
provider: 'lighttpd'
},

plugins: {
lighttpd: {
url: "http://example.com/flowplayer.pseudostreaming-byterange-3.2.11.swf"
}

}
});
</script>
</div>

Answer

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:

/url:\s+"([^"]+)"/

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];
}