Domanda

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
È stato utile?

Soluzione

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";
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top