如何用Selenium定位TradingView相机按钮并点击Tweet Image?
解决TradingView中Selenium定位相机按钮并点击Tweet Image的问题
绕过右键无法检查元素的限制
如果右键无法调出检查选项,直接用快捷键打开Chrome开发者工具:
- Windows/Linux:
F12或Ctrl+Shift+I - Mac:
Cmd+Opt+I
调出后切换到Elements标签,用左上角的选择元素工具(箭头图标)点击相机按钮,即可定位到对应的DOM节点。
相机按钮的常见定位方式(基于TradingView当前DOM结构)
TradingView的相机按钮属于图表工具栏组件,可用以下几种稳定的选择器定位:
- 基于aria标签(推荐,类名变更时仍有效):
button[aria-label*="Camera"] - 基于功能属性:
.chart-toolbar button[data-name="screenshot"] - 基于图标类组合:
.icon-ZdW2cL9I.icon-camera-ZdW2cL9I
注意:TradingView的类名可能随版本更新变化,建议用开发者工具确认最新选择器。
修改后的完整代码实现
在原有代码中添加相机按钮定位方法,并调整执行流程:
import time from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By # 常量配置 DRIVER_PATH = 'path_to_chromedriver' EMAIL = 'my_email_id' PWD = 'my_password' class Browser: def __init__(self, driver: str, keep_open: bool) -> None: self.service = Service(driver) chrome_options = Options() chrome_options.add_experimental_option("detach", keep_open) chrome_options.add_experimental_option('excludeSwitches', ['enable-logging']) self.driver = webdriver.Chrome(service=self.service, options=chrome_options) def open_page(self, url: str): self.driver.get(url) self.driver.maximize_window() def close(self): time.sleep(5) print("shutting down browser") self.driver.close() def sign_in(self): # 打开登录页 self.open_page("https://www.tradingview.com/#signin") # 点击邮箱登录按钮 button = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.NAME, "Email"))) button.click() # 输入账号密码并登录 self.driver.find_element(By.ID, "id_username").send_keys(EMAIL) self.driver.find_element(By.ID, "id_password").send_keys(PWD) sign_in_btn = self.driver.find_element(By.CSS_SELECTOR, ".submitButton-LQwxK8Bm.button-D4RPB3ZC.size-large-D4RPB3ZC.color-brand-D4RPB3ZC.variant-primary-D4RPB3ZC.stretch-D4RPB3ZC") sign_in_btn.click() def click_products_tab(self): products_tab = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Products"))) products_tab.click() def click_camera_button(self): # 等待相机按钮加载完成并点击 camera_btn = WebDriverWait(self.driver, 15).until( EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[aria-label*="Camera"]')) ) camera_btn.click() def click_tweet_image(self): tweet_image = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Tweet Image"))) tweet_image.click() def save_chart_img(self): ActionChains(self.driver).key_down(Keys.ALT).send_keys('s').perform() element = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, 'Save image'))) element.click() browser = Browser(DRIVER_PATH, True) # 执行流程 browser.sign_in() browser.click_products_tab() browser.click_camera_button() # 先打开截图菜单 browser.click_tweet_image() # 再点击Tweet Image选项
额外问题处理
如果相机按钮在iframe内,需先切换到对应iframe再定位:
# 切换到图表iframe(示例选择器,需根据实际DOM调整) WebDriverWait(self.driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[src*='chart']")))
内容的提问来源于stack exchange,提问作者Samaara
相关产品推荐
相关产品推荐

