[英]Get text from XPath located Element using Selenium WebDriver with JavaScript
[英]How to get a specific text from a javascript code in selenium webdriver by xpath
我想獲得一段特定的文本,即此代碼“ clip:”下的“ URL”。
我試過了
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>
我認為您無法從xpath獲取url。
收到文本后,就像以前一樣,應該使用正則表達式來獲取URL。
也許是這樣的:
/url:\s+"([^"]+)"/
這將保存匹配項的網址。
這是一個有關如何集成到代碼中的示例:
// 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];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.