简体   繁体   中英

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM