【问题标题】:Unable to locate element within an iframe while the element actually exists当元素实际存在时,无法在 iframe 中定位元素
【发布时间】:2022-02-08 04:50:33
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: python selenium xpath iframe css-selectors


    【解决方案1】:

    元素 在线观看<iframe> 内,因此您必须:

    • 诱导WebDriverWait 使所需的框架可用并切换到它

    • 诱导WebDriverWait 使所需的元素可点击

    • 您可以使用以下任一Locator Strategies

      • 使用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
      

    参考

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2017-07-12
      相关资源
      最近更新 更多