0

在 Selenium 中,我可以通过以下方式定位项目及其 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
4

1 回答 1

1

您使用 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";
}
于 2014-01-21T16:32:31.317 回答