0

I am scraping Tradingview.com and I'm trying to get my code to right click on the chart but it can't do it.

This is my code:

# import modules
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

# class
class OpenChart:

  def __init__(self, driver, entry, tp, sl, symbol, timeframe) -> None:
    self.driver = driver
    self.entry = entry
    self.tp = tp
    self.sl = sl
    self.symbol = symbol
    self.timeframe = timeframe
    self.old_tab_handle = self.driver.window_handles[0]

  def open_new_tab(self):
    # Opening a duplicate tab
    self.driver.execute_script('''window.open("{}");'''.format(self.driver.current_url))  
    self.new_tab_handle = self.driver.window_handles[1]

  def set_up_new_tab(self):
    # right click and click the "remove all indicators" option
    chart = WebDriverWait(self.driver, 15).until(EC.presence_of_element_located((By.TAG_NAME, "canvas")))
    ActionChains(self.driver).context_click(chart).perform()

    menu = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div[data-name="menu-inner"] table tbody')))
    options = menu.find_elements(By.CSS_SELECTOR, 'tr.item-GJX1EXhk.interactive-GJX1EXhk.normal-GJX1EXhk')
    options[11].click()

  def switch_to_old_tab(self):
    self.driver.switch_to.window(self.old_tab_handle)

  def switch_to_new_tab(self):
    self.driver.switch_to.window(self.new_tab_handle)
    

This is the html code of the dropdown option's menu:

<div class="menuBox-Kq3ruQo8" data-name="menu-inner">
   <table>
      <tbody>
         <tr class="item-GJX1EXhk interactive-GJX1EXhk normal-GJX1EXhk">
            <td class="iconCell-GJX1EXhk" data-icon-cell="true">
               <span class="icon-GJX1EXhk">
                  <svg xmlns="http://www.w3.org/2000/svg" width="28" height="28">
                     <g fill="none" fill-rule="evenodd" stroke="currentColor">
                        <path d="M6.5 15A8.5 8.5 0 1 0 15 6.5H8.5"></path>
                        <path d="M12 10L8.5 6.5 12 3"></path>
                     </g>
                  </svg>
               </span>
            </td>
            <td>
               <div class="content-GJX1EXhk"><span class="label-GJX1EXhk" data-label="true">Reset chart view</span><span class="shortcut-GJX1EXhk">Alt + R</span></div>
            </td>
         </tr>
         <tr class="subMenu-GJX1EXhk">
            <td></td>
         </tr>
         <tr class="row-DFIg7eOh">
            <td>
               <div class="line-DFIg7eOh"></div>
            </td>
            <td>
               <div class="line-DFIg7eOh"></div>
            </td>
         </tr>
         <tr class="item-GJX1EXhk interactive-GJX1EXhk normal-GJX1EXhk">
            <td class="iconCell-GJX1EXhk" data-icon-cell="true"></td>
            <td>
               <div class="content-GJX1EXhk"><span class="label-GJX1EXhk" data-label="true">Copy price 1.30655</span></div>
            </td>
         </tr>
         <tr class="subMenu-GJX1EXhk">
            <td></td>
         </tr>
         <tr class="item-GJX1EXhk interactive-GJX1EXhk normal-GJX1EXhk">
            <td class="iconCell-GJX1EXhk" data-icon-cell="true"></td>
            <td>
               <div class="content-GJX1EXhk"><span class="label-GJX1EXhk" data-label="true">Paste</span><span class="shortcut-GJX1EXhk">Ctrl + V</span></div>
            </td>
         </tr>
         <tr class="subMenu-GJX1EXhk">
            <td></td>
         </tr>
         <tr class="row-DFIg7eOh">
            <td>
               <div class="line-DFIg7eOh"></div>
            </td>
            <td>
               <div class="line-DFIg7eOh"></div>
            </td>
         </tr>
         <tr class="item-GJX1EXhk interactive-GJX1EXhk normal-GJX1EXhk">
            <td class="iconCell-GJX1EXhk" data-icon-cell="true">
               <span class="icon-GJX1EXhk">
                  <svg xmlns="http://www.w3.org/2000/svg" width="28" height="28">
                     <g fill="none" fill-rule="evenodd" stroke="currentColor">
                        <path stroke-linecap="square" d="M21.5 14.5a8 8 0 1 0-8 8m6-6v8m-4-4h8"></path>
                        <path d="M10 14.5h3.5V9m-3.277-1.76a3 3 0 1 0-3.993 3.979m14.565-.012a3 3 0 1 0-4.019-3.966"></path>
                     </g>
                  </svg>
               </span>
            </td>
            <td>
               <div class="content-GJX1EXhk"><span class="label-GJX1EXhk" data-label="true">Add alert on GBPUSD at 1.30655…</span><span class="shortcut-GJX1EXhk">Alt + A</span></div>
            </td>
         </tr>
         <tr class="subMenu-GJX1EXhk">
            <td></td>
         </tr>
         <tr class="item-GJX1EXhk interactive-GJX1EXhk normal-GJX1EXhk" data-action-name="trade">
            <td class="iconCell-GJX1EXhk" data-icon-cell="true"></td>
            <td>
               <div class="content-GJX1EXhk">
                  <span class="label-GJX1EXhk" data-label="true">Trade</span>
                  <span class="arrowIcon-GJX1EXhk" data-submenu-arrow="true">
                     <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 16" width="10" height="16">
                        <path d="M.6 1.4l1.4-1.4 8 8-8 8-1.4-1.4 6.389-6.532-6.389-6.668z"></path>
                     </svg>
                  </span>
               </div>
            </td>
         </tr>
         <tr class="subMenu-GJX1EXhk">
            <td></td>
         </tr>
         <tr class="row-DFIg7eOh">
            <td>
               <div class="line-DFIg7eOh"></div>
            </td>
            <td>
               <div class="line-DFIg7eOh"></div>
            </td>
         </tr>
         <tr class="item-GJX1EXhk interactive-GJX1EXhk hovered-GJX1EXhk normal-GJX1EXhk">
            <td class="iconCell-GJX1EXhk" data-icon-cell="true">
               <span class="icon-GJX1EXhk">
                  <svg xmlns="http://www.w3.org/2000/svg" width="28" height="28">
                     <g fill="none" fill-rule="evenodd">
                        <path stroke="currentColor" d="M13 22.5H5.5a2 2 0 0 1-2-2v-14a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2V14"></path>
                        <path stroke="currentColor" stroke-linecap="square" d="M18.5 15.5v8m-4-4h8"></path>
                        <path fill="currentColor" d="M7 8h11v1H7zm0 4h11v1H7zm0 4h5v1H7z"></path>
                     </g>
                  </svg>
               </span>
            </td>
            <td>
               <div class="content-GJX1EXhk">
                  <span class="label-GJX1EXhk" data-label="true">Add GBPUSD to watchlist</span>
                  <span class="arrowIcon-GJX1EXhk" data-submenu-arrow="true">
                     <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 16" width="10" height="16">
                        <path d="M.6 1.4l1.4-1.4 8 8-8 8-1.4-1.4 6.389-6.532-6.389-6.668z"></path>
                     </svg>
                  </span>
               </div>
            </td>
         </tr>
         <tr class="subMenu-GJX1EXhk">
            <td>
               <div class="menu-Tx5xMZww context-menu menuWrap-Kq3ruQo8" style="position: fixed; left: 527.514px; top: 183.068px;">
                  <div class="scrollWrap-Kq3ruQo8" style="overflow-y: auto;">
                     <div class="menuBox-Kq3ruQo8" data-name="menu-inner">
                        <table>
                           <tbody>
                              <tr class="item-GJX1EXhk interactive-GJX1EXhk normal-GJX1EXhk">
                                 <td class="iconCell-GJX1EXhk" data-icon-cell="true"></td>
                                 <td>
                                    <div class="content-GJX1EXhk">
                                       <span class="label-GJX1EXhk" data-label="true">
                                          <div class="item-MkbKmfsi item-jFqVJoPk">
                                             <span class="labelRow-jFqVJoPk label-MkbKmfsi">
                                                <span class="label-jFqVJoPk">
                                                   <label class="checkbox-aOSYFxuH checkbox-MkbKmfsi checkbox-vyj6oJxw">
                                                      <span class="wrapper-GZajBGIm">
                                                         <input class="input-GZajBGIm" type="checkbox" value="on" checked="">
                                                         <span class="box-GZajBGIm check-GZajBGIm">
                                                            <span class="icon-GZajBGIm">
                                                               <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 11 9" width="11" height="9" fill="none">
                                                                  <path stroke-width="2" d="M0.999878 4L3.99988 7L9.99988 1"></path>
                                                               </svg>
                                                            </span>
                                                         </span>
                                                      </span>
                                                      <span class="label-vyj6oJxw">Watchlist</span>
                                                   </label>
                                                </span>
                                             </span>
                                          </div>
                                       </span>
                                       <span class="shortcut-GJX1EXhk invisibleHotkey-GJX1EXhk">Alt + W</span>
                                    </div>
                                 </td>
                              </tr>
                              <tr class="subMenu-GJX1EXhk">
                                 <td></td>
                              </tr>
                              <tr class="row-DFIg7eOh">
                                 <td>
                                    <div class="line-DFIg7eOh"></div>
                                 </td>
                                 <td>
                                    <div class="line-DFIg7eOh"></div>
                                 </td>
                              </tr>
                              <tr class="item-GJX1EXhk interactive-GJX1EXhk normal-GJX1EXhk">
                                 <td class="iconCell-GJX1EXhk" data-icon-cell="true"></td>
                                 <td>
                                    <div class="content-GJX1EXhk"><span class="label-GJX1EXhk" data-label="true">Create new list…</span></div>
                                 </td>
                              </tr>
                              <tr class="subMenu-GJX1EXhk">
                                 <td></td>
                              </tr>
                           </tbody>
                        </table>
                     </div>
                  </div>
               </div>
               <div style="position: fixed; z-index: 150; left: 0px; top: 0px;"></div>
            </td>
         </tr>
         <tr class="item-GJX1EXhk interactive-GJX1EXhk normal-GJX1EXhk">
            <td class="iconCell-GJX1EXhk" data-icon-cell="true">
               <span class="icon-GJX1EXhk">
                  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 28 28" width="28" height="28">
                     <path fill="currentColor" d="M15 5H6.5C5.67 5 5 5.67 5 6.5v15c0 .83.67 1.5 1.5 1.5h15c.83 0 1.5-.67 1.5-1.5V16h1v5.5a2.5 2.5 0 0 1-2.5 2.5h-15A2.5 2.5 0 0 1 4 21.5v-15A2.5 2.5 0 0 1 6.5 4H15v1Zm7.41-.3a2 2 0 0 0-2.82 0l-.94.95-7.5 7.5-1 1-.15.14V19h4.7l.15-.15 1-1 7.5-7.5.94-.94a2 2 0 0 0 0-2.82L22.41 4.7Zm-2.12.71a1 1 0 0 1 1.42 0l1.88 1.88a1 1 0 0 1 0 1.42l-.59.58-1.65-1.64L19.71 6l.58-.59Zm.36 2.94L22.29 10l-6.79 6.8-1.65-1.65-1.64-1.65L19 6.7l1.65 1.65Zm-7.5 7.5 1.64 1.65-.5.5H11v-3.3l.5-.5 1.65 1.65Z"></path>
                  </svg>
               </span>
            </td>
            <td>
               <div class="content-GJX1EXhk"><span class="label-GJX1EXhk" data-label="true">Add text note for GBPUSD</span><span class="shortcut-GJX1EXhk">Alt + N</span></div>
            </td>
         </tr>
         <tr class="subMenu-GJX1EXhk">
            <td></td>
         </tr>
         <tr class="row-DFIg7eOh">
            <td>
               <div class="line-DFIg7eOh"></div>
            </td>
            <td>
               <div class="line-DFIg7eOh"></div>
            </td>
         </tr>
         <tr class="item-GJX1EXhk interactive-GJX1EXhk normal-GJX1EXhk">
            <td class="iconCell-GJX1EXhk" data-icon-cell="true"><span class="icon-GJX1EXhk"></span></td>
            <td>
               <div class="content-GJX1EXhk"><span class="label-GJX1EXhk" data-label="true">Lock vertical cursor line by time</span></div>
            </td>
         </tr>
         <tr class="subMenu-GJX1EXhk">
            <td></td>
         </tr>
         <tr class="row-DFIg7eOh">
            <td>
               <div class="line-DFIg7eOh"></div>
            </td>
            <td>
               <div class="line-DFIg7eOh"></div>
            </td>
         </tr>
         <tr class="item-GJX1EXhk interactive-GJX1EXhk normal-GJX1EXhk">
            <td class="iconCell-GJX1EXhk" data-icon-cell="true"></td>
            <td>
               <div class="content-GJX1EXhk"><span class="label-GJX1EXhk" data-label="true">Object Tree…</span></div>
            </td>
         </tr>
         <tr class="subMenu-GJX1EXhk">
            <td></td>
         </tr>
         <tr class="item-GJX1EXhk interactive-GJX1EXhk normal-GJX1EXhk" data-action-name="apply-color-theme">
            <td class="iconCell-GJX1EXhk" data-icon-cell="true">
               <span class="icon-GJX1EXhk">
                  <svg xmlns="http://www.w3.org/2000/svg" width="28" height="28">
                     <path fill="currentcolor" d="m13.5 5a1 1 0 0 0 0 19 1 1 0 0 0 0-19m0 1a1 1 0 0 1 0 17 1 1 0 0 1 0-17zm2.31-.6A9.5 9.5 0 0 0 9 14.5a9.5 9.5 0 0 0 6.81 9.1l.99-.78A8.5 8.5 0 0 1 10 14.5a8.5 8.5 0 0 1 6.8-8.32"></path>
                  </svg>
               </span>
            </td>
            <td>
               <div class="content-GJX1EXhk">
                  <span class="label-GJX1EXhk" data-label="true">Color Theme</span>
                  <span class="arrowIcon-GJX1EXhk" data-submenu-arrow="true">
                     <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 16" width="10" height="16">
                        <path d="M.6 1.4l1.4-1.4 8 8-8 8-1.4-1.4 6.389-6.532-6.389-6.668z"></path>
                     </svg>
                  </span>
               </div>
            </td>
         </tr>
         <tr class="subMenu-GJX1EXhk">
            <td></td>
         </tr>
         <tr class="row-DFIg7eOh">
            <td>
               <div class="line-DFIg7eOh"></div>
            </td>
            <td>
               <div class="line-DFIg7eOh"></div>
            </td>
         </tr>
         <tr class="item-GJX1EXhk interactive-GJX1EXhk disabled-GJX1EXhk normal-GJX1EXhk">
            <td class="iconCell-GJX1EXhk" data-icon-cell="true"></td>
            <td>
               <div class="content-GJX1EXhk"><span class="label-GJX1EXhk" data-label="true">Remove drawings</span></div>
            </td>
         </tr>
         <tr class="subMenu-GJX1EXhk">
            <td></td>
         </tr>
         <tr class="item-GJX1EXhk interactive-GJX1EXhk normal-GJX1EXhk">
            <td class="iconCell-GJX1EXhk" data-icon-cell="true"></td>
            <td>
               <div class="content-GJX1EXhk"><span class="label-GJX1EXhk" data-label="true">Remove indicators</span></div>
            </td>
         </tr>
         <tr class="subMenu-GJX1EXhk">
            <td></td>
         </tr>
         <tr class="row-DFIg7eOh">
            <td>
               <div class="line-DFIg7eOh"></div>
            </td>
            <td>
               <div class="line-DFIg7eOh"></div>
            </td>
         </tr>
         <tr class="item-GJX1EXhk interactive-GJX1EXhk normal-GJX1EXhk">
            <td class="iconCell-GJX1EXhk" data-icon-cell="true">
               <span class="icon-GJX1EXhk">
                  <svg xmlns="http://www.w3.org/2000/svg" width="28" height="28">
                     <g fill="none" fill-rule="evenodd" stroke="currentColor" transform="translate(4 4)">
                        <path d="M.5 10.992c0 .287.226.508.505.508H2.65c.19.93.55 1.82 1.09 2.63L2.577 15.3a.5.5 0 0 0-.007.71l1.42 1.42a.5.5 0 0 0 .71-.007l1.17-1.163c.81.54 1.71.9 2.63 1.09v1.645c0 .284.227.505.508.505h1.984c.28 0 .508-.221.508-.505V17.35a7.46 7.46 0 0 0 2.63-1.09l1.17 1.163a.5.5 0 0 0 .71.007l1.42-1.42a.5.5 0 0 0-.007-.71l-1.163-1.17c.54-.81.9-1.7 1.09-2.63h1.645a.502.502 0 0 0 .505-.508V9.008a.503.503 0 0 0-.505-.508H17.35c-.19-.93-.55-1.82-1.09-2.63l1.163-1.17a.5.5 0 0 0 .007-.71l-1.42-1.42a.5.5 0 0 0-.71.007L14.13 3.74a7.46 7.46 0 0 0-2.63-1.09V1.005A.504.504 0 0 0 10.992.5H9.008a.504.504 0 0 0-.508.505V2.65c-.92.19-1.82.55-2.63 1.09L4.7 2.577a.5.5 0 0 0-.71-.007L2.57 3.99a.5.5 0 0 0 .007.71L3.74 5.87c-.54.81-.9 1.7-1.09 2.63H1.005a.503.503 0 0 0-.505.508v1.984z"></path>
                        <circle cx="10" cy="10" r="2.5"></circle>
                     </g>
                  </svg>
               </span>
            </td>
            <td>
               <div class="content-GJX1EXhk"><span class="label-GJX1EXhk" data-label="true">Settings…</span></div>
            </td>
         </tr>
         <tr class="subMenu-GJX1EXhk">
            <td></td>
         </tr>
      </tbody>
   </table>
</div>

the html code is for this menu and the circled dropdown option is the one which I'm trying to click: dropdown menu

In the set_up_new_tab() method, I'm trying to right click on the chart and then click a dropdown menu option. The option which I'm trying to click is options[11].

8
  • Update the question with the text based HTML of the element. Commented Jul 17, 2023 at 15:57
  • Done. Let me know if I have to do anything else. Commented Jul 17, 2023 at 16:37
  • This question isn't about right-click using selenium at all and falls under I wrote some buggy code that I can’t fix. Because when I did driver.get("https://in.tradingview.com/chart/?symbol=CRYPTO%3ABTCUSD"), OpenChart(driver, '', '', '', '', '').set_up_new_tab() then it performed as expected. Check this if you want more insight on how to debug Commented Jul 17, 2023 at 20:50
  • Got it, @DanielSabrel. Thanks for giving the link on how to debug. I needed that. :) Commented Jul 18, 2023 at 9:37
  • @Samaara While running code Reset chart view option wasn't available for me so it opened settings instead. So, you might want to find element by text as mentioned by undetected selenium in answers to be less error prone and avoid confusion. Commented Jul 18, 2023 at 9:43

1 Answer 1

0

To right click within the canvas within TradingView website and select the context menu with text Remove indicators you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategies:

  • Code block:

    driver.get("https://www.tradingview.com/#signin")
    # perform signin and the initial steps
    chart = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.chart-gui-wrapper > canvas")))
    ActionChains(driver).context_click(chart).perform()
    WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Remove indicators']")))
    
  • Note: You have to add the following imports :

    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.