0

I tried searching for similar issues on the web but couldnt find an answer for this specific element of the website I am trying to access. A similar approach worked for other elements, hence trying to get some help please.

I would like to download the file located under the Download button there using Selenium in python https://www.sgx.com/derivatives/negotiated-large-trade

The button seems to be located under:

<span class="table-action-label" title="Download" data-i18n="[title]sgx-table.toolbar.action-download;[text]sgx-table.toolbar.action-download" data-i18n-options="{}">Download</span>

My code is

import selenium
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

from fake_useragent import UserAgent

ua = UserAgent()
ua_rand = ua.random

print(ua_rand)


dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = ua_rand

browser = webdriver.PhantomJS("C://, desired_capabilities=dcap)
browser.get("https://www.sgx.com/derivatives/negotiated-large-trade")

browser.find_element_by_xpath("//span[@class='table-action-label']").click()


browser.quit()

But it doesnt seem to find the element

NoSuchElementException: {"errorMessage":"Unable to find element with xpath '//span[@class='table-action-label']'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Content-Length":"119","Content-Type":"application/json

How could i download the file?

many thanks

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Thieb
  • 1
  • 1

3 Answers3

0

did u try using this xpath : /html/body/div[1]/main/div[1]/article/template-base/div/div/sgx-widgets-wrapper/widget-derivatives-nlt/section[1]/div[1]/sgx-table/sgx-table-toolbar/div[2]/span

Bullet
  • 28
  • 3
0

To click on the element with text as Download you need to:

  • Induce WebDriverWait for the invisibility_of_element() sgx-loader.

  • Induce WebDriverWait for the element_to_be_clickable()

  • You can use either of the following Locator Strategies:

    • Using LINK_TEXT:

      driver.get("https://www.sgx.com/derivatives/negotiated-large-trade")
      WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.TAG_NAME, "sgx-loader")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Download"))).click()
      
    • Using CSS_SELECTOR:

      driver.get("https://www.sgx.com/derivatives/negotiated-large-trade")
      WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.TAG_NAME, "sgx-loader")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.table-action-label[title='Download']"))).click()
      
    • Using XPATH:

      driver.get("https://www.sgx.com/derivatives/negotiated-large-trade")
      WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.TAG_NAME, "sgx-loader")))    
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='table-action-label' and @title='Download'][text()='Download']"))).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
    
  • Browser Snapshot: sgx


References

You can find a couple of relevant discussions on NoSuchElementException in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • thank you for taking the time to help, I have tried all 3 and get the same the result of a: TimeoutException I am using Phantomjs, could it be the issue? – Thieb Dec 22 '20 at 13:31
0

thank you both for your help, so far one version that works using google chrome is below. I am still trying to have it working using phantomJs.

import selenium
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time

from fake_useragent import UserAgent

ua = UserAgent()
ua_rand = ua.random
driver2 = webdriver.Chrome(executable_path=r"C:\Users\chromedriver.exe")

driver2.get("https://www.sgx.com/derivatives/negotiated-large-trade")
WebDriverWait(driver2, 20).until(EC.invisibility_of_element((By.TAG_NAME, "sgx-loader"))) 
time.sleep(5)
print("Downloading...")   
WebDriverWait(driver2, 60).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='table-action-label' and @title='Download'][text()='Download']"))).click()

time.sleep(3)

driver2.quit()
Thieb
  • 1
  • 1