polishfreak
polishfreak

Reputation: 49

How to click javascript:void(0) link with selenium?

I am using selenium and chrome to automate the clicks of a page with python.

I am getting stuck not being able to click on the following href link:

<a href="javascript:void(0)" class="addSuppData-trigger pts" data-target="edit_3-1" style="padding-right:6px;float:right;">
            <i class="material-icons black-text tiny-small">edit</i></a>

I have tried using xpath, css, and linktext to no avail.

sample code:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="row-group-1"]/td[1]/div/div[2]/a/i' ))).click()

The goal is to click the pen, then select the item from the drop down. screen shot of button

The second highlight line is the pen. html tree

Upvotes: 1

Views: 5161

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193088

The element seems to be a dynamic element and to click on it you need to induce WebDriverWait for the desired element to be clickable and you can use either of the following solutions:

  • Using CSS_SELECTOR:

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.addSuppData-trigger.pts[data-target^='edit_']>i.material-icons.black-text.tiny-small"))).click()
    
  • Using XPATH:

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='addSuppData-trigger pts' and starts-with(@data-target, 'edit_')]/i[@class='material-icons black-text tiny-small' and contains(., 'edit')]"))).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
    

Upvotes: 1

Related Questions