1

I have a problem to make the function continue without the error NoSuchElementException.In this case the function is Selenium and it clicks on one point on the page to select all the files and the other to download the files, however sometimes the search has no files to click or no items to download, and it gives an error of NoSuchElementException. How do I pass it and continue the function without stopping the function? However the function stop with the error: selenium.common.exceptions.NoSuchElementException

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
sleep(2)
select_all = driver.find_element(
By.XPATH, '//*[@id="ng-form:ng-detail-dataTable:ng-data-table:selectionMode"]/div/div[2]')
select_all.click()
sleep(5)
download_all = driver.find_element(
By.XPATH, '//*[@id="ng-form:ng-toolbar:dowloadXmlAction"]')
download_all.click()
sleep(2)
GC Py
  • 11
  • 1
  • 1) Is the `select_all` element remains clickable when the search has no files? 2) Possibly the `download_all` element is disabled/absent when the search has no files. Can you please confirm? – undetected Selenium Feb 08 '23 at 22:42

3 Answers3

0

Simplest way is to use find_elements instead of find_element, because it always returns a list, so if it doesn't find anything returns an empty list, while find_element raise error if it doesn't find the element.

select_all = driver.find_elements(
By.XPATH, '//*[@id="ng-form:ng-detail-dataTable:ng-data-table:selectionMode"]/div/div[2]')
if select_all:
    select_all[0].click()
    sleep(5)
    download_all = driver.find_element(
    By.XPATH, '//*[@id="ng-form:ng-toolbar:dowloadXmlAction"]')
    download_all.click()
    sleep(2)

where if select_all: means "if select_all is not empty"

sound wave
  • 3,191
  • 3
  • 11
  • 29
0

You can use a try-except block and perform the click there

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException


select_all = driver.find_element(
By.XPATH, '//*[@id="ng-form:ng-detail-dataTable:ng-data-table:selectionMode"]/div/div[2]')
try:
    select_all.click()
except NoSuchElementException:
    print('Element was not present')
Override12
  • 116
  • 1
  • 7
0

Assuming the select_all element is always clickable/selectable irrespective of any files(s) is/are present or not and the download_all element remains invisible/disables when there is/are no file available to be downloaded you can wrap the click on download_all within a try-except block catching NoSuchElementException and you can use the following locator strategies:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

select_all = driver.find_element(By.XPATH, '//*[@id="ng-form:ng-detail-dataTable:ng-data-table:selectionMode"]/div/div[2]')
select_all.click()
sleep(5)
try:
    download_all = driver.find_element(By.XPATH, '//*[@id="ng-form:ng-toolbar:dowloadXmlAction"]')
    download_all.click()
except NoSuchElementException:
    pass
sleep(2)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352