使用 Selenium 在网页上处理 iframe

Handling with iframe on a webpage using Selenium

我想用我的脚本从 investing.com 上的图表中抓取一些数据,但首先我需要在数据 抓取 之前准备图表(图表最大化。切换在蜡烛条上)。 我知道需要的按钮位于 iframe 元素内。不幸的是我的脚本找不到这些元素。我尝试通过“frame_to_be_available_and_switch_to_it”定位 iframe,但脚本仍然找不到 iframe,因此找不到所需的元素。我的代码如下:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.expected_conditions import visibility_of_element_located

PATH = "C:\Program Files (x86)\chromedriver.exe"
options = Options()
options.headless = False
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")


driver = webdriver.Chrome(PATH, options=options)
action = ActionChains(driver)


def cookie_handling():
    driver.get("https://www.investing.com/equities/inpost-sa-as-chart")
    COOKIE_BUTTON = By.ID, 'onetrust-accept-btn-handler'
    cookie_button = WebDriverWait(driver, 5).until(EC.presence_of_element_located(COOKIE_BUTTON))
    cookie_button.click()
    print("Cookie handling finalized")


def chart_preparing():
    scroll_down = driver.find_element_by_xpath('/html/body')
    for i in range(19):
        scroll_down.send_keys(Keys.ARROW_DOWN)

    FRAME_LOCATOR = By.CLASS_NAME, "fwh mp0 no-overflow"
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(FRAME_LOCATOR))

    FULLSCREEN_LOCATOR = By.CLASS_NAME, "button fullscreen iconed"
    WebDriverWait(driver, 10).until((EC.element_to_be_clickable(FULLSCREEN_LOCATOR))).click()

    CANDLE_LOCATOR = By.XPATH, "/html/body/div[1]/div[2]/div/div/div[2]/div[3]/div/div/span[1]"
    WebDriverWait(driver, 10).until((EC.element_to_be_clickable(CANDLE_LOCATOR))).click()


cookie_handling()
chart_preparing()

我收到以下错误:

selenium.common.exceptions.TimeoutException: Message: 

好吧,那里有 3 个 iframe,而您所有的 iframe 定位器都是错误的。
其他定位器也应该改进。
请试试这个:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.expected_conditions import visibility_of_element_located

PATH = "C:\Program Files (x86)\chromedriver.exe"
options = Options()
options.headless = False
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")


driver = webdriver.Chrome(PATH, options=options)
action = ActionChains(driver)
wait = WebDriverWait(driver, 20)


def cookie_handling():
    driver.get("https://www.investing.com/equities/inpost-sa-as-chart")
    COOKIE_BUTTON = By.ID, 'onetrust-accept-btn-handler'
    cookie_button = WebDriverWait(driver, 5).until(EC.presence_of_element_located(COOKIE_BUTTON))
    cookie_button.click()
    print("Cookie handling finalized")


def chart_preparing():
    scroll_down = driver.find_element_by_xpath('/html/body')
    for i in range(19):
        scroll_down.send_keys(Keys.ARROW_DOWN)

    wait.until(EC.frame_to_be_available_and_switch_to_it(By.XPATH, "//iframe[@seamless and not( @class)]"))

    wait.until(EC.frame_to_be_available_and_switch_to_it(By.XPATH, "//iframe[@seamless and @class]"))
    wait.until(EC.frame_to_be_available_and_switch_to_it(By.XPATH, "//iframe[contains(@id,'tradingview')]"))

    FULLSCREEN_LOCATOR = By.CLASS_NAME, "button fullscreen iconed"
    wait.until((EC.element_to_be_clickable(FULLSCREEN_LOCATOR))).click()

    CANDLE_LOCATOR = By.XPATH, "//span[@title='Candles']"
    wait.until((EC.element_to_be_clickable(CANDLE_LOCATOR))).click()


cookie_handling()
chart_preparing()