Getting current video tag URL with selenium

2024/10/5 17:26:36

I'm trying to get the current html5 video tag URL using selenium (with python bindings):

from selenium import webdriverdriver = webdriver.Chrome()
driver.get('https://www.youtube.com/watch?v=9x6YclsLHN0')video = driver.find_element_by_tag_name('video')
url = driver.execute_script("return arguments[0].currentSrc;", video)
print urldriver.quit()

The problem is that the url value is printed empty. Why is that and how can I fix it?


I suspect that this is because the script is executed and the currentSrc value is returned before the video tag has been initialized. I've tried to add an Explicit Wait, but still got an empty string printed:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ECwait = WebDriverWait(driver, 5)
video = wait.until(EC.visibility_of_element_located((By.TAG_NAME, 'video')))

Which makes me feel I need to do it asynchronously. May be listening for the media events and wait for the video to start playing.

I'm also pretty sure currentSrc should work, because if I execute the code in the console and manually wait for a video to start - I see it printing the video currentSrc attribute value.


FYI, also tried with java bindings, same result, an empty string:

WebDriver driver = new ChromeDriver();
driver.get("https://www.youtube.com/watch?v=9x6YclsLHN0");WebElement video = driver.findElement(By.tagName("video"));JavascriptExecutor js = (JavascriptExecutor) driver;
String url = (String) js.executeScript("return arguments[0].currentSrc;", video);System.out.println(url);
Answer

According to the W3 video tag specification:

The currentSrc DOM attribute is initially the empty string. Its valueis changed by the resource selection algorithm.

Which explains the behavior described in the question. This also means that to get the currentSrc value reliably, we need to wait until the media resource has it defined.

Subscribing to the loadstart media event through execute_async_script() did the trick:

driver.set_script_timeout(10) url = driver.execute_async_script("""var video = arguments[0],callback = arguments[arguments.length - 1];video.addEventListener('loadstart', listener);function listener() {callback(video.currentSrc);};
""", video)
print(url)
https://en.xdnf.cn/q/119552.html

Related Q&A

How to determine if two rows are identical (similar) if row 2 contains part of the info from row 1?

Hope you are having a good day. I am currently working with an extremely dirty dataframe containing First Name, Last Name, and Middle Name. One the issues that I am trying to resolve looks like below:F…

Cartopy fancy box

Hello I have been trying to plot data in a Orthographic projection. The data is plotted but I want the box to follow the data limits. Like in this example I am sharing form M_map[enter image descriptio…

discord.py - No DM sent to the user

I am making a discord.Client. I have a DM command that sends a DM to a specific user, but no message is sent to the user when the command is run, but a message is sent on the Context.channel. Here is m…

Improve CPU time of conditional statement

I have written an if-elif statement, which I believe not be very efficient:first_number = 1000 second_number = 700 switch = {upperRight: False,upperLeft: False,lowerRight: False,lowerLeft: False,middle…

Why no colon in forming a list from loop in one line in Python?

From this website, there is a way to form a list in Python from loop in one line squares = [i**2 for i in range(10)]My question is, typically, after a loop, there is a colon, e.g., squares = [] for i i…

Merge each groups rows into one row

Im experienced with Pandas but stumbled upon a problem that I cant seem to figure out. I have a large dataset ((40,000, 16)) and I am trying to group it by a specific column ("group_name" for…

Python decode unknown character

Im trying to decode the following: UKLTD� For into utf-8 (or anything really) but I cannot workout how to do it and keep getting errors likeascii codec cant decode byte 0xae in position 8: ordinal not…

UnboundLocalError: TRY EXCEPT STATEMENTS

I am currently creating a menu with try except tools. Im trying to create it so if a user enters nothing (presses ENTER) to output:You have not entered anything, please enter a number between 1 and 4Th…

Cant load music into pygame

please help if you can. Cant seem to be able to upload music into my game in progress. It comes up with the error of "cant load"... Would be great if someone got back to me quick, This is a m…

C# Socket: how to keep it open?

I am creating a simple server (C#) and client (python) that communicate using sockets. The server create a var listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp)then …