newbie
newbie

Reputation: 3

Using Selenium and xpath to find the source link to an MP4 video on a web site e.g. Fmovies

selenium and xpath. I have a python script that uses selenium to scrape the video source from a movie website. I can get the script to play the video using Selenium but want to scrape the src link to the MP4 video file. I think my xpath syntax is incorrect.

**** Code ******
# Load selenium components
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import time

browser = webdriver.Chrome(executable_path=r"C:\\temp\\chromedriver.exe")

## Link to the movie as an example
url = "https://vw.ffmovies.sc/film/fatman-2020/watching/?server_id=3"
browser.get(url)
element = WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='player']")))
clickable = browser.find_element_by_id("player")
clickable.find_element_by_xpath('.//*').click()
browser.switch_to.frame("iframe-embed") 
time.sleep(5)

### This is where I am stuck.. It cannot find the xpath element.... 
##########################################################################################
## I am getting the xpath wrong. I want the video link to be stored in the link variable. 
link=browser.switch_to.frame(browser.find_element_by_xpath('//*[@id="player"]/iframe').get_attribute('src')) 
## Getting error in the above code                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

browser.close()

Any advice will be much appreciated. Thanks.

Upvotes: 0

Views: 1901

Answers (1)

Alexey R.
Alexey R.

Reputation: 8676

id='player' is outside the iframe so that you shouldn't use it in your xpath.

enter image description here

So you should consider the iframe as the root of your new context.

Instead of browser.find_element_by_xpath('//*[@id="player"]/iframe').get_attribute('src') try:

browser.find_element_by_xpath('.//video/source').get_attribute('src')

Upvotes: 1

Related Questions