我想使用我的腳本從 Investment.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:
uj5u.com熱心網友回復:
好吧,那里有 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()
uj5u.com熱心網友回復:
我看到您正在使用帶空格的類名。
在 Selenium 中,如果您使用這樣的類名
fwh mp0 no-overflow
你會得到 no such element exception.
使固定 :
最好洗掉空格并放置句點而不是形成CSS SELECTOR.
FRAME_LOCATOR = By.CSS_SELECTOR, ".fwh.mp0.no-overflow"
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(FRAME_LOCATOR))
FULLSCREEN_LOCATOR = By.CSS_SELECTOR, ".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 按鈕,所以我將該代碼包裝在
try-except. 因為我不確定,您可能會根據地理位置獲得 cookie 按鈕。我看到有一個視頻彈出視窗。我們也應該關閉它。
有嵌套的 iframe。
所以你的有效代碼將是:
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
wait = WebDriverWait(driver, 30)
def cookie_handling():
driver.get("https://www.investing.com/equities/inpost-sa-as-chart")
try:
COOKIE_BUTTON = By.ID, 'onetrust-accept-btn-handler'
cookie_button = wait.until(EC.presence_of_element_located(COOKIE_BUTTON))
cookie_button.click()
print("Cookie handling finalized")
except:
pass
def chart_preparing():
driver.execute_script("window.scrollTo(0, 500)")
try:
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[name()='svg' and @id='Layer_1']"))).click()
except:
pass
FIRST_FRAME_LOCATOR = By.XPATH, "//iframe[starts-with(@id,'tvc_frame_')]"
wait.until(EC.frame_to_be_available_and_switch_to_it(FIRST_FRAME_LOCATOR))
SECOND_FRAME_LOCATOR = By.XPATH, "//iframe[starts-with(@src,'https://tvc') and not(@id)]"
wait.until(EC.frame_to_be_available_and_switch_to_it(SECOND_FRAME_LOCATOR))
THIRD_FRAME_LOCATOR = By.XPATH, "//iframe[starts-with(@src,'https://tvc') and contains(@id,'tradingview')]"
wait.until(EC.frame_to_be_available_and_switch_to_it(THIRD_FRAME_LOCATOR))
CANDLE_LOCATOR = By.XPATH, "//div[@class='quick']/span"
wait.until((EC.element_to_be_clickable(CANDLE_LOCATOR))).click()
cookie_handling()
chart_preparing()
進口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/362069.html
