0

I have the following HTML code:

...
<div class="media ipad_media" style="padding-top: 0;">
    <iframe src="//www.youtube.com/embed/XXXXXXXX" frameborder="0" allowfullscreen="allowfullscreen" width="618" height="330" data-gtm-yt-inspected-7182449_30="true"> 
    </iframe>
<div>
...
<div class="media ipad_media hidden-xs">
   <iframe src="//www.youtube.com/embed/XXXXXXXX" frameborder="0" allowfullscreen="allowfullscreen" width="618" height="330" data-gtm-yt-inspected-7182449_30="true">
   </iframe>
</div>

I want the src attribute which is actually the same in both iframes. I just locate the first element by using the following command:

elem = driver.find_element_by_class_name("media.ipad_media").find_element_by_tag_name("iframe")    

I then excute the following:

print(elem.get_attribute('width'))
print(elem.get_attribute('frameborder'))
print(elem.get_attribute('allowfullscreen'))
print(elem.get_attribute('src'))
print(elem.get_attribute('source'))
print(elem.text)
print(elem.tag_name)

I get the following in the console:

618
0
true

None

iframe

How is it possible to get nothing executing print(elem.get_attribute('src'))? As you understand, executing the $$("div.media.ipad_media>iframe") command in the console gives 2

Billy Grande
  • 577
  • 3
  • 11
  • 23
  • What locator are you using to locate the IFRAME? Please add that to your question. My guess is that the page has more than one IFRAME and you didn't end up with the one you were looking for. – JeffC Jan 25 '21 at 23:02
  • Also: the src could be dynamically loaded on the page and you could be fetching it before it's populated. – DMart Jan 25 '21 at 23:26
  • @JeffC I have edited the description accordingly – Billy Grande Jan 26 '21 at 17:42
  • @DMart I have put it to sleep for 5s before printing the src – Billy Grande Jan 26 '21 at 17:43
  • `"media.ipad_media"` is not a class name. It's actually two class names (which won't work in `find_element_by_class_name()`) but you've mixed in some CSS selector syntax (the `.` between the two class names). I would suggest that you change the chained `find_element()` calls to just `elem = driver.find_element_by_css_selector("div.media.ipad_media>iframe")`. – JeffC Jan 26 '21 at 22:33
  • @JeffC I have changed the locator but I get exactly the same outcome. – Billy Grande Jan 27 '21 at 18:43
  • Update your question with the current code you are using. Are there any other IFRAMEs on the page? Have you tried running `$$("div.media.ipad_media>iframe")` in the dev console and confirm that only 1 element is found? – JeffC Jan 27 '21 at 18:47
  • @JeffC There are actually 2 iframes. I have edited the description accordingly. – Billy Grande Jan 27 '21 at 21:53

1 Answers1

-1

The <div> tag is the immediate ancestor of the desired <iframe> tag.

To print the value of the src attribute of the <iframe> you can use either of the following Locator Strategies:

  • Using css_selector:

    print(driver.find_element_by_css_selector("div.media.ipad_media > iframe").get_attribute("src"))
    
  • Using xpath:

    print(driver.find_element_by_xpath("//div[@class='media ipad_media']/iframe").get_attribute("src"))
    

Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.media.ipad_media > iframe"))).get_attribute("src"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='media ipad_media']/iframe"))).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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352