approximatenumber
approximatenumber

Reputation: 507

Cannot get dynamical element with python selenium

There is site, that streams youtube videos. I want to get playlist with them. So I use selenium webdriver to get the needed element div with class-name ytp-title-text where youtube link is located.

It is located here for example, when I use browser console to find element:

<div class="ytp-title-text"><a class="ytp-title-link yt-uix-sessionlink" target="_blank" data-sessionlink="feature=player-title" href="https://www.youtube.com/watch?v=VyCY62ElJ3g">Fears - Jono McCleery</a><div class="ytp-title-subtext"><a class="ytp-title-channel-name" target="_blank" href=""></a></div></div>

I wrote simple script for testing:

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

driver = webdriver.Firefox()
driver.get('http://awsmtv.com')

try:
    element = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.CLASS_NAME, "ytp-title-text"))
    )
finally:
    driver.quit()

But no element is found and timeout exception is thrown. I cannot understand, what actions selenium needs to perform to get the full page source.

Upvotes: 2

Views: 204

Answers (2)

Rain9333
Rain9333

Reputation: 570

Just saw this element is inside iframe... You need to switch to the iframe first -> find it by ClassName -> ifame = ...(By.CLASS_NAME, "player") then switch to it driver.switch_to_frame(iframe) and you should be able now to get the wanted element :)

The XPath locator like this one will work (or your locator) -> "//a[@class='ytp-title-link yt-uix-sessionlink']".

You then need via the element to get the property href for the youtube video url or the text of the element for the song title.

If still not working I can suggest to get the page source - html = driver.page_source which will give you the source of the page and via some regex to get the info you want eventually.

Upvotes: 1

Andersson
Andersson

Reputation: 52675

Required link is hidden and also located inside an iframe. Try below to locate it:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it("tvPlayer_1"))
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CLASS_NAME, "ytp-title-link")))
    print(element.get_attribute('href'))
finally:
    driver.quit()

Upvotes: 2

Related Questions