Cyber Avater
Cyber Avater

Reputation: 2147

Unable to locate element within an iframe while the element actually exists

No matter how long I wait, it seems selenium can't find the "watch_online" button.

I've tried both by XPath, full XPath, and CSS selector.

I want to get the href link from the "Watch Online" button.

import os
import glob
import time

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait

# Create local browser cache
browser_data_location = "browser_data"
options = webdriver.ChromeOptions()
options.add_argument(f'user-data-dir={os.getcwd()}/{browser_data_location}')

i_m_not_a_robot_xpath = '//*[@id="landing"]/div[2]/center/img'
generate_link_xpath = '//*[@id="generater"]/img'
click_to_continue = '//*[@id="showlink"]'
get_download_link = '/html/body/section/div/div/div/center/a'
watch_online = '//*[@id="download-hidden"]/a'

with webdriver.Chrome(options=options, ) as driver:
    wait = WebDriverWait(driver, 10)

    time.sleep(2)

    driver.get(
        "https://www.rtilinks.com/?82255aba71=RmwzVDZObDFBdDQvay8zRjhiaStoM004Ymd1T201MnBQelJpdW5oK1UxeGFvbFZUY1FEVXMrY0o2UnhqeGxOOFlwN3JlUElad2h0ek9pQ1ZFZndXSG9UTzA1aFpmTEhoanBVUldEYWwwWVU9")

    # wait.until(ec.element_to_be_clickable((By.CSS_SELECTOR, upload_box_css))).send_keys(file)

    wait.until(ec.element_to_be_clickable((By.XPATH, i_m_not_a_robot_xpath))).click()
    # time.sleep(1)
    wait.until(ec.element_to_be_clickable((By.XPATH, generate_link_xpath))).click()
    wait.until(ec.element_to_be_clickable((By.XPATH, click_to_continue))).click()
    # original_window = driver.current_window_handle

    driver.close()
    driver.switch_to.window(driver.window_handles[0])

    wait.until(ec.element_to_be_clickable((By.XPATH, get_download_link))).click()


    time.sleep(2)
    link = driver.find_element(By.XPATH, watch_online)
    print(link.get_attribute('href'))

Upvotes: 1

Views: 679

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193088

The element Watch Online is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use either of the following Locator Strategies:

    • Using PARTIAL_LINK_TEXT:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'https://purefiles.in')]")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Watch Online"))).click()
      
    • Using CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://purefiles.in']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.button.is-success"))).click()
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='button is-success' and contains(., 'Watch Online')]"))).click()
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'https://purefiles.in')]")))
      
  • 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
    

Reference

You can find a couple of relevant discussions in:

Upvotes: 1

Related Questions