GSV
GSV

Reputation: 39

How to extract the 'src' attribute as per the html through Selenium and Java

I have a html page:

<video crossorigin="anonymous" class="" id="video" playsinline="true" 
       src="https://df.dfs.bnt.com/
       DEEAB832j06E9j413FjAA8Dj2zxc535DA2072E3jW01j15579/mp4- 
       hi/jFNf2IbBoGF28IzyU_WqkA,1535144598/zxxx/
       contents/1/8/1a57ae021173751b468cca136e0192.mp4? 
       rnd=0.38664488150364296"> 
</video>

Through Selenium WebDriver I tried get video url:

By videoLocator = By.id("video");
WebElement videoElement = driver.findElement(videoLocator);
String videoUrl = videoElement.getAttribute("src");

But videoUrl - always returns ""(is empty).

Нowever for example:

videoElement.getAttribute("crossorigin")

return correct answer: "anonymous".

I have tried get this url from src attribute using javascript:

String videoUrl = (String) js.executeScript("return document.getElementById( 'video' ).getAttribute( 'src' );");

But the result is still the same: "".

I guess that the problem is in crossorigin="anonymous" but what to do with it? How can I get src value?

Sorry, for my poor English.

Upvotes: 2

Views: 644

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193088

As per the HTML you have provided you need to induce WebDriverWait and you can use the following solution:

WebElement videoElement = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//video[@id='video' and @crossorigin='anonymous'][starts-with(@src,'http')]")));
System.out.println(videoElement.getAttribute("src"));

Upvotes: 1

HemSa
HemSa

Reputation: 176

Try fetching innerHTML attribute. Code :

By videoLocator = By.id("video");
WebElement videoElement = driver.findElement(videoLocator);
String videoUrl = videoElement.getAttribute("innerHTML");

Upvotes: 0

Related Questions