3

I try to use Selenium to perform click Download button at Historical Data/Time and Sales Historical Data section in webpage: https://www.sgx.com/research-education/derivatives.

I tried the code below:

sgxMainPageUrl = "https://www.sgx.com/research-education/derivatives"
# downloadButtonClassName = "sgx-button--primary"
downloadButtonXpath = "//widget-reports-derivatives-tick-and-trade-cancellation//button[contains(@class,'sgx-button--primary')]"

driver = webdriver.Chrome(chromedriver_path)
driver.get(sgxMainPageUrl)


# search = driver.find_element_by_class_name(downloadButtonClassName)
search = driver.find_element_by_xpath(downloadButtonXpath)

But it returns:

File "...\\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//widget-reports-derivatives-tick-and-trade-cancellation//button[contains(@class,'sgx-button--primary')]"}

I read several post and they faced this problem because duplicate xpath but this xpath is unique. Please help me explain why, many thanks!

2 Answers 2

2

There's an Accept cookies button, you need to click first. after that you can use the below xpath to click on download button :

(//button[text()='Download'])[1]

Code :

driver.get(sgxMainPageUrl)
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[class*='banner-acceptance-button']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "(//button[text()='Download'])[1]"))).click()

Imports :

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

1

You should add a wait to let the page loaded before accessing that element.
Try this:

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

wait = WebDriverWait(driver, 20)

wait.until(EC.visibility_of_element_located((By.XPATH, downloadButtonXpath))).click()

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.