Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Selenium can't find element by Class Name and xpath

Tags:

selenium

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!

like image 524
Dang Huy Nguyen Avatar asked Jul 17 '26 20:07

Dang Huy Nguyen


2 Answers

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
like image 190
cruisepandey Avatar answered Jul 21 '26 03:07

cruisepandey


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()

like image 44
Prophet Avatar answered Jul 21 '26 05:07

Prophet