double clicking an element with Selenim in Python isn't working

43 views Asked by At

I'm scraping Tradingview. On that website is this chart: enter image description here

I'm trying to double click the red circled element on the chart. When my code tries to double click it, it doesn't get double clicked.

This is the code which I tried (it's a method inside a class):

def change_indicator_settings(self, entry, tp, sl):
    # double click on the indicator
    indicator = self.driver.find_elements(By.CSS_SELECTOR, 'div[data-name="legend-source-item"]')
    print(indicator[0])
    print(indicator[0].get_attribute('class'))
    ActionChains(self.driver).double_click(indicator[0]).perform()

There is nothing wrong with my selector or the code which I'm using, so, I don't know why this isn't working.

Here is the html of the element which I'm trying to double click:



<div class="sources-131H9iuA"> 
  <div class="item-131H9iuA last-131H9iuA blockHidden-e6PF69Df"></div>
  <div class="item-131H9iuA study-131H9iuA has5Buttons-131H9iuA withTail-131H9iuA" data-name="legend-source-item"></div> 
  <div class="item-131H9iuA study-131H9iuA disabled-131H9iuA has5Buttons-131H9iuA" data-name="legend-source-item"></div>
</div>

Can someone tell me what I'm doing wrong and how I can fix it

2

There are 2 answers

0
Ξένη Γήινος On

Don't know about why your code doesn't work, since your code seems mostly correct and I cannot find any chart like the one you posted when I click the link.

But you can try using xpath instead of css selector:

def change_indicator_settings(self, entry, tp, sl):
    indicator = self.driver.find_element('xpath', '//div[@data-name="legend-source-item"]')
    ActionChains(self.driver).double_click(indicator).perform()

If your CSS selector failed to find the elements, the XPATH method will find the elements.

If it didn't work because no elements were found, then the above should work.

But here there are two elements that will be selected with the above XPATH, there are possibly more. Assume there are two such elements and your code failed because you need to double click the second one, use the following:

def change_indicator_settings(self, entry, tp, sl):
    indicator = self.driver.find_element('xpath', '(//div[@data-name="legend-source-item"])[2]')
    ActionChains(self.driver).double_click(indicator).perform()

If ActionChains.double_click doesn't work, try use indicator.click() twice instead (indicator.click(); indicator.click())

So there four tests you need to do. Beware some of the tests might raise exceptions, as I don't have access to the chart in question I cannot test it. But whatever exception you encounter, it means the method that caused it doesn't work. Just ignore the exception and use the other methods.

0
Samaara On

Turns out there was nothing wrong with what my code. I just had to fix something in that red circled element.

Also, I changed my code just a little bit. I moved to that element before I double clicked on it.

def change_indicator_settings(self, entry, tp, sl):
    # get the 1st indicator on the top of the chart
    indicators = self.driver.find_elements(By.CSS_SELECTOR, 'div[data-name="legend-source-item"]')

    # move to the element
    ActionChains(self.driver).move_to_element(indicators[0]).perform()

    # double click on it
    ActionChains(self.driver).double_click(indicators[0]).perform()