0

I have this button

<button _ngcontent-gyx-c3="" class="btn btn-gradient-primary btn-icon-text p-3 col-12" routerlink="/certificate-request/create" routerlinkactive="active" type="button" tabindex="0"><i _ngcontent-gyx-c3="" class="icon-plus btn-icon-prepend"></i> New request </button>

how can i sleect it for seinuim test I tried this :

driver.find_element_by_xpath('//button[@class="btn btn-gradient-primary btn-icon-text p-3 col-12"][.="New request"]').click()

But I have the following exception:

no such element: Unable to locate element: {"method":"xpath","selector":"//button[@class="btn btn-gradient-primary btn-icon-text p-3 col-12"][.="New request"]"} (Session info: headless chrome=72.0.3626.109)

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
rota90
  • 249
  • 1
  • 4
  • 17

3 Answers3

0

There are multiple xpath patters that could work, this is one that's close to the one in your question.

driver.find_element_by_xpath('//button[@class="btn btn-gradient-primary btn-icon-text p-3 col-12"][text(),"New request"]')
0buz
  • 3,443
  • 2
  • 8
  • 29
0

The desired element is a Angular element so to click on the element using Selenium 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, "button.btn.btn-gradient-primary.btn-icon-text[routerlink='/certificate-request/create'][routerlinkactive='active']>i.icon-plus.btn-icon-prepend"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@routerlink='/certificate-request/create' and @routerlinkactive='active']/i[@class='icon-plus btn-icon-prepend']"))).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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
-1

Do you have the option to put an id on your button and do it like this?

driver.find_element_by_id(<id_of_button>)

EDIT:

How about this?

driver.find_element_by_xpath('//button[@class="icon-plus btn-icon-prepend"][.="New request"]').click()
Klatten
  • 307
  • 3
  • 16