1

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.

Here is how it looks like: snapshot of "Take a snapshot" dropdown

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.

Samaara
  • 27
  • 7

1 Answers1

1

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352