2016-10-22 9 views
0

このコードの "clip:"の下に特定の行のテキストを取得したいとします。xpathでセレンウェブドライブのjavascriptコードから特定のテキストを取得する方法

私は

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

を試してみましたが、それは全体のテキストを取得しています。私はURLを取得したい。これにはどんな解決策がありますか?

<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を取得するために正規表現を使用する必要があります。

たぶん、このような何か:

/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

ありがとうございます。 –

関連する問題