2016-10-22 49 views
0

我想获得特定的文本行,该代码的“clip:”下的“URL”。如何从xpath中的selenium webdriver的javascript代码中获取特定的文本

我试图

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

,但它是获得整个文本。我只想获取网址。有没有解决这个问题的方法?

<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> 

回答

1

我认为你无法从xpath获取URL。

当你有了文字之后,你应该使用正则表达式来获取你的网址。

也许是这样的:

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

这将节省匹配的URL。

这里就介绍如何在你的代码集成了一个例子:

// 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]; 
} 
+0

我完全新的正则表达式我以前从来没有听说过这个词。你能写出完整的代码吗?谢谢 –

+0

谢谢你。 –

相关问题