0

despite numerous tries I'm unable to perform the following action(s).

I need to land on a page that contains one or more table rows/columns. For each (sequentially) I need to click on an arrow that opens a pop-up, close the window, then rinse and repeat.

Problem: I am unable to click on the item

Error:

class 'selenium.common.exceptions.NoSuchElementException'

Snippet of code that prompts the error:

[...]
    driver = webdriver.Chrome('chromedriver.exe')
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--incognito")


    # MODIFY URL HERE
    driver.get(url)
[..]
try:
        # arf = driver.find_element_by_name("ctl00_ContentPlaceHolder1_d_dettaglio_ctl02_button1").click()
        arf = driver.find_element_by_xpath('//input[@id="ctl00_ContentPlaceHolder1_d_dettaglio_ctl02_button1"]').click()
        pprint.pprint(arf)
    except NoSuchElementException:
        print ("error!", NoSuchElementException)
        driver.close()
        exit()

HTML element I need to interact with:

                        <td align="center">
                            <input type="image" name="ctl00$ContentPlaceHolder1$d_dettaglio$ctl02$button1" id="ctl00_ContentPlaceHolder1_d_dettaglio_ctl02_button1" src="../images/spunta_maggiore.gif" style="height:22px;width:22px;border-width:0px;">
                        </td>

Things I've tried:

  • driver.find_element_by_xpath (//input[@id etc...]) or driver.find_element_by_xpath('//input[@name])
  • driver.find_element_by_name("ctl00_ContentPlaceHolder1_d_dettaglio_ctl02_button1").click()
  • driver.find_element_by_id("ctl00_ContentPlaceHolder1_d_dettaglio_ctl02_button1").click()

In both cases, a no element found exception is raised.

What am I doing wrong ?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
user3375601
  • 119
  • 2
  • 9
  • Have you tried waiting for the element to load? e.g. `WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, 'xpath of your choice here'))).click()` – 0buz Jun 12 '20 at 10:00

1 Answers1

0

The desired element is a dynamic element so to click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[type='image'][name*='ContentPlaceHolder'][id*='d_dettaglio'][src*='images/spunta_maggiore']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[contains(@name, 'ContentPlaceHolder') and contains(@id, 'd_dettaglio')][@type='image' and contains(@src, 'images/spunta_maggiore.gif')]"))).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
    

References

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

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