I would like to use my script to grab some data from charts on investing.com, but first I need to prepare chart before data scrapping (chart maximizing. switching on candle bars). I know that needed buttons are located inside iframe element. Unfortunately my script cannot find these elements. I tried locate iframe by "frame_to_be_available_and_switch_to_it" but script still cannot find iframe and thus required elements. My code is as follows:
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()
I get the following error:
selenium.common.exceptions.TimeoutException: Message:
CodePudding user response:
I see you are using class name with spaces.
In Selenium if you use the class name like this
fwh mp0 no-overflow
You are gonna get no such element exception.
Fix :
Better to remove the spaces and put period instead to form a 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()
CodePudding user response:
Well, there are 3 iframes there and all your iframe locators are wrong.
Also other locator should be improved.
Please try this:
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)
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.XPATH, "//iframe[@seamless and not( @class)]"))
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.XPATH, "//iframe[@seamless and @class]"))
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.XPATH, "//iframe[contains(@id,'tradingview')]"))
FULLSCREEN_LOCATOR = By.CLASS_NAME, "button fullscreen iconed"
WebDriverWait(driver, 10).until((EC.element_to_be_clickable(FULLSCREEN_LOCATOR))).click()
CANDLE_LOCATOR = By.XPATH, "//span[@title='Candles']"
WebDriverWait(driver, 10).until((EC.element_to_be_clickable(CANDLE_LOCATOR))).click()
cookie_handling()
chart_preparing()