Home > Back-end >  Python Selenium - How to select this button and click it
Python Selenium - How to select this button and click it

Time:08-18

I've been trying to get this button selected and click it in order to add a trading view strategy to a chart. I've automated everything up to this point including the locating and clicking of similar buttons. This one seems a bit finicky. Here is the code that I've already implemented that works (except for the 'Add To Chart' button)

WHAT WORKS ON OTHER BUTTONS

# Select the strategy
select_strat = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CLASS_NAME, 'title-phaedJZ1')))
select_strat.click()

# Close the strategy window
close_strat_window = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CLASS_NAME, 'close-tuOy5zvD')))
close_strat_window.click()

WHAT I'M TRYING TO LOCATE AND CLICK enter image description here

WHAT I'VE ALREADY TRIED IMPLEMENTING

# Add strat to chart
# button-TuYnJRjv rightControlsBlock__button-TuYnJRjv button-9pA37sIi isInteractive-9pA37sIi newStyles-9pA37sIi ace_layer ace_cursor-layer ace_hidden-cursors
add_to_chart = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="tv-script-pine-editor-header-root"]/div[1]/div/div[2]/div[3]')))
add_to_chart.click()

Note - I've tried every combination of the comment of the class below for CLASS_NAME

# Add strat to chart
# button-TuYnJRjv rightControlsBlock__button-TuYnJRjv button-9pA37sIi isInteractive-9pA37sIi newStyles-9pA37sIi ace_layer ace_cursor-layer ace_hidden-cursors
add_to_chart = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'button-TuYnJRjv')))
add_to_chart.click()

REQUEST FROM COMMENTS FOR STACKTRACE

pipenv run python .\dev\tv_data_collector.py
Loading .env environment variables...
C:\Users\REDACTED\Documents\Trading\Automation\dev\tv_data_collector.py:20: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver = webdriver.Chrome(executable_path=path, chrome_options=options)
C:\Users\tgall\Documents\Trading\Automation\dev\tv_data_collector.py:20: DeprecationWarning: use options instead of chrome_options
  driver = webdriver.Chrome(executable_path=path, chrome_options=options)

DevTools listening on ws://127.0.0.1:65270/devtools/browser/93062f9b-ac9d-465e-9471-df4c5098e655
[26756:18156:0817/175454.408:ERROR:device_event_log_impl.cc(214)] [17:54:54.409] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
Traceback (most recent call last):
  File "C:\Users\REDACTED\Documents\Trading\Automation\dev\tv_data_collector.py", line 126, in <module>
    tv_get_security(driver)
  File "C:\Users\REDACTED\Documents\Trading\Automation\dev\tv_data_collector.py", line 73, in tv_get_security
    script_search = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'input-CcsqUMct')))
  File "C:\Users\REDACTED\.virtualenvs\Automation--luj7l49\lib\site-packages\selenium\webdriver\support\wait.py", line 90, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
Backtrace:
        Ordinal0 [0x006E78B3 2193587]
        Ordinal0 [0x00680681 1771137]
        Ordinal0 [0x005941A8 803240]
        Ordinal0 [0x005C24A0 992416]
        Ordinal0 [0x005C273B 993083]
        Ordinal0 [0x005EF7C2 1177538]
        Ordinal0 [0x005DD7F4 1103860]
        Ordinal0 [0x005EDAE2 1170146]
        Ordinal0 [0x005DD5C6 1103302]
        Ordinal0 [0x005B77E0 948192]
        Ordinal0 [0x005B86E6 952038]
        GetHandleVerifier [0x00990CB2 2738370]
        GetHandleVerifier [0x009821B8 2678216]
        GetHandleVerifier [0x007717AA 512954]
        GetHandleVerifier [0x00770856 509030]
        Ordinal0 [0x0068743B 1799227]
        Ordinal0 [0x0068BB68 1817448]
        Ordinal0 [0x0068BC55 1817685]
        Ordinal0 [0x00695230 1856048]
        BaseThreadInitThunk [0x76466739 25]
        RtlGetFullPathName_UEx [0x778890AF 1215]
        RtlGetFullPathName_UEx [0x7788907D 1165]

This error has not prevented anything from working going forward (until now perhaps.. not sure)

CodePudding user response:

in your ficture, div.js-button-text.text-9pA37sli is CSS SELECTOR so

EC.presence_of_element_located((By.CLASS_NAME, 'button-TuYnJRjv')) into EC.presence_of_element_located((By.CSS_SELECTOR, 'div.js-button-text.text-9pA37sli'))

CodePudding user response:

If you are clicking on the button then you should use element to be clickable instead of presece of element like below:

add_to_chart = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, 'button-TuYnJRjv')))
add_to_chart.click()

OR

add_to_chart = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[normalize-space()='Add to chart']")))
add_to_chart.click()

OR

add_to_chart = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(text(),'Add to chart')]")))
add_to_chart.click()

For DeprecationWarning the Selenium v4 compatible Code Block

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.google.com")

For more info visit this

  • Related