在硒中,我可以找到一个物品,其html这样:
driver.get('http://www.google.com/ncr');
driver.findElement(webdriver.By.id('hplogo')).getAttribute('outerHTML').then(
function(html) {
console.log(html);
});
我是否有可能检索我要获得的HTML的文件类型?例如,如果HTML记录如下:
<video src="http://www.myvideo.com/video.webm"></video>
我会得到以下输出:
webm
您正在使用getAttribute函数找到的只是字符串。
实际的文件类型,如果目前还没有找到它。
但是,在您的示例中,您现在拥有包含您要查找的文件的字符串,并且可以使用Java来缩小最后一部分文件名。
String type;
String attribute = driver.findElement(webdriver.By.id('hplogo')).getAttribute('outerHTML');
int dotLocation = attribute.lastIndexOf(".");
if(dotLocation != -1 && dotLocation != attribute.length -1){
type = attribute.substring(dotLocation + 1, attribute.length());
} else {
type = "Unknown";
}