문제

In Selenium, I can locate an item and its HTML this way:

driver.get('http://www.google.com/ncr');

driver.findElement(webdriver.By.id('hplogo')).getAttribute('outerHTML').then(
  function(html) {
    console.log(html);
  });

Is it possible for me to retrieve the file type of the HTML I am getting? For example, if the HTML is logged as follows:

<video src="http://www.myvideo.com/video.webm"></video>

I would get the following output:

webm
도움이 되었습니까?

해결책

What you are finding with the getAttribute function is merely a string.

The actual file type and if it exists at all are not found at this point.

However, in your example, you now have the string that contains the file you are looking for and can use java to substring off the last part filename.

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";
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top