如何单击一个我看不到它的元素的硒框?

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

我想将fxblue技术分析中的帧时间从1h(默认值)更改为5m,但我无法点击它的弹出按钮。 这是我试过的代码:

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

此时我可以看到带有时间范围的弹出窗口,但找不到更改时间范围的方法。

Timeframe 弹出窗口中的元素位于 iframe 中。需要switch to frame与其中包含的元素进行交互。

# 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()

单击该元素会打开一个对话框。
因此,您需要 select 并在该对话框中单击所需的元素。
对话框在iframe中,你必须切换到那个iframe。
select输入所需选项并关闭对话框后,您必须从 iframe 切换回默认内容。
此外,您应该使用显式等待而不是硬编码暂停。
你的代码可能是这样的

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