元素实际存在时无法在 iframe 中定位元素

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

无论我等多久,selenium 似乎都找不到“watch_online”按钮。

我已经尝试过 XPath、完整的 XPath 和 CSS 选择器。

我想从“在线观看”按钮获取 href link。

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'))

元素 在线观看 位于 内,因此您必须:

  • 诱导 WebDriverWait 所需的 帧可用并切换到它.

  • 诱导 所需的 元素可点击

  • 您可以使用以下任一项:

    • 使用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()
      
    • 使用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()
      
    • 使用 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')]")))
      
  • 注意:您必须添加以下导入:

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

参考

您可以在以下位置找到一些相关讨论:

  • Switch to an iframe through Selenium and python