Home > Mobile >  How to click a box with selenium which i can't see its element?
How to click a box with selenium which i can't see its element?

Time:12-20

I want to change the frametime in fxblue technical analysis from 1h (the default value) to 5m but i can't click its pop-up button. here is the code i tried:

import pandas as pd
import numpy as np
import csv
import os
from selenium import webdriver
driver = webdriver.Chrome(os.getcwd()   '/chromedriver')  
url = "https://www.fxblue.com/market-data/technical-analysis/EURUSD"
driver.get(url)
time.sleep(5)
timestamp = driver.find_element_by_xpath('//*[@id="TimeframeContainer"]').click()

at this point I can see the pop-up with the timeframes but I couldn't find a way to change the timeframe.

CodePudding user response:

The elements in the Timeframe pop-up is in an iframe. Need to switch to frame to interact with the elements contained in it.

# Imports required
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver.get("https://www.fxblue.com/market-data/technical-analysis/EURUSD")

wait = WebDriverWait(driver,30)

# Click on timestamp button.
timestamp = wait.until(EC.element_to_be_clickable((By.ID,"txtTimeframe")))
timestamp.click()

# Switch to iframe.
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[contains(@class,'DialogInnerIframe')]")))

# click on M5 button.
fivemin = wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@class='TimeframeItem' and text()='M5']")))
fivemin.click()

# Switch to default content to interact with other elements.
driver.switch_to.default_content()

CodePudding user response:

Clicking on that element opens a dialog.
So you need to select and click the desired element on that dialog.
The dialog is in iframe, you have to switch to that iframe.
After selecting the desired option and the dialog is closed you have to switch back from the iframe to the default content.
Also you should use explicit wait instead of hardcoded pauses.
Your code may be something like this

import pandas as pd
import numpy as np
import csv
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(os.getcwd()   '/chromedriver')  
wait = WebDriverWait(driver, 20)

url = "https://www.fxblue.com/market-data/technical-analysis/EURUSD"
driver.get(url)
wait.until(EC.visibility_of_element_located((By.ID, "TimeframeContainer"))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.DialogDragBar")))
driver.find_element_by_xpath("//div[@tf='300']").click()
driver.switch_to.default_content()
  • Related