Get link video from website by selenium. How to get the link?

i want to get video link from website https://www.ofw.su/family-feud-july-29-2022 but i can't. This my code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import time
from datetime import datetime
from random import randint
import random
import string
import os

def get(link):
   CHROMEDRIVER_PATH = 'chromedriver.exe'
   options = webdriver.ChromeOptions()
   options.add_argument("user-data-dir=E:\\profile")
   options.add_argument("--disable-notifications")
   #options.add_argument("--headless")
   options.add_experimental_option('excludeSwitches', ['enable-logging'])
   driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH,options=options)
   driver.get(link)
   time.sleep(2)
   url_video = driver.find_element_by_xpath("/html/body/div/div[2]/div[3]/video").get_attribute('src')
   print(url_video)
   return url_video


link = "https://www.ofw.su/family-feud-july-29-2022"
get(link)

I didn't get any links

Upvotes: 0

Views: 805

Answers (1)

Prophet
Prophet

Reputation: 33361

The element you are trying to access is inside the iframe.
So, in order to access elements inside the iframe you have to switch to that iframe as follows:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import time
from datetime import datetime
from random import randint
import random
import string
import os

def get(link):
   CHROMEDRIVER_PATH = 'chromedriver.exe'
   options = webdriver.ChromeOptions()
   options.add_argument("user-data-dir=E:\\profile")
   options.add_argument("--disable-notifications")
   #options.add_argument("--headless")
   options.add_experimental_option('excludeSwitches', ['enable-logging'])
   driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH,options=options)
   driver.get(link)
   time.sleep(2)
   iframe = driver.find_element_by_xpath("//iframe[@class='embed-responsive-item']")
   driver.switch_to.frame(iframe)
   url_video = driver.find_element_by_xpath("/html/body/div/div[2]/div[3]/video").get_attribute('src')
   print(url_video)
   return url_video


link = "https://www.ofw.su/family-feud-july-29-2022"
get(link)

When you finish working with elements inside the iframe, in order to switch to the regular content you should do that with the following code:

driver.switch_to.default_content()

Also, you should use explicit waits instead of hardcoded delays time.sleep(2) and use relative locators, not the absolute XPaths like this /html/body/div/div[2]/div[3]/video

Upvotes: 1

Related Questions