-1

Here's the HTML

<div class="bg_tv col-md-12 online">
  <iframe width="100%" height="460" src="https://www.youtube.com/embed/dtKciwk_si4" scrolling="OFF" frameborder="0" allowfullscreen=""></iframe></div>

I tried:

browser.find_element(By.TAG_NAME, "iframe").get_attribute("src")

But I got:

no such element: Unable to locate element: {"method":"css selector","selector":"iframe"}

and then I tried XPATH:

browser.find_element(By.XPATH,"/html/body/div/div[3]/div/div[2]/div[1]/iframe").get_attribute("src")

But got unable to locate element error as well. Any help please?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Amr
  • 119
  • 1
  • 10

2 Answers2

2

Browser runners like selenium can be difficult because the runner may be executing the script before all elements of a page are loaded first. I would try adding a wait for the element first. As provided by the python-selenium documentation at https://selenium-python.readthedocs.io/waits.html#explicit-waits

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.TAG_NAME, "iframe"))
    )
finally:
    driver.quit()

This should get your element if it's loading in the browser context.

0

To print the value of the src attribute of the <iframe> you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using XPATH:

    print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='bg_tv col-md-12 online']/iframe[@src]"))).get_attribute("src"))
    
  • Using CSS_SELECTOR:

    print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.bg_tv.col-md-12.online > iframe[src]"))).get_attribute("src"))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

References

You can find a couple of relevant discussions on NoSuchElementException in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352