英文:
How do I click on the "Tweet Image" button on Tradingview?
问题
# 以下是我的现有python代码:
# 一些常量
DRIVER_PATH = 'path_to_chromedriver'
EMAIL = 'my_email_id'
PWD = 'my_password'
# ...
# 在这里我卡住了
希望找到相机按钮的CSS路径。我尝试右键点击并检查它,但每次右键点击时,我都没有找到检查选项。
英文:
I am trying to access this website: Tradingview.com
I wrote a piece of Python code which uses selenium to do access it. After accessing the website, my goal is to click on the "Tweet Image" button. In order to do so, I need to first find the camera buttons' css path so that I can click on it. I cannot find the css path to the camera button.
This is my existing python code:
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
# some constants
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):
# open the sign in page
self.open_page("https://www.tradingview.com/#signin")
# click the email button
button = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.NAME, "Email")))
button.click()
# enter email, password & click sign in
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_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)
# sign in
browser.sign_in()
# click on "Products" tab
browser.click_products_tab()
# click on "Tweet Image" button
# I'm stuck here
I need help finding the css path of that camera button. I have tried right-clicking and inspecting it but everytime I right click, I don't get an option to inspect it.
答案1
得分: 1
- 使用 CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Take a snapshot'] > div#header-toolbar-screenshot"))).click()
- 使用 XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Take a snapshot']/div[@id='header-toolbar-screenshot']"))).click()
- 注意:您需要添加以下导入语句:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
英文:
To click on the camera button element within TradingView website you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
-
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Take a snapshot'] > div#header-toolbar-screenshot"))).click()
-
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Take a snapshot']/div[@id='header-toolbar-screenshot']"))).click()
-
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论