1

Hi please help i have to find the xpath of this

<button _ngcontent-c27="" aria-label="VALIDATE" color="primary" fxflex="" mat-raised-button="" type="submit" class="mat-raised-button mat-primary" style="flex: 1 1 0%; box-sizing: border-box;"><span class="mat-button-wrapper"> VALIDATE </span><div class="mat-button-ripple mat-ripple" matripple=""></div><div class="mat-button-focus-overlay"></div></button>

but as i copy the xpath

/html/body/app-root/app-side-nav/mat-sidenav-container/mat-sidenav-content/main/app-otp/app-page-container/div/form/div/div/form/div/div[1]/button

then when i use it the console error NoSuchElementException

this is my code in selenium thanks in advace

driver.findElement(By.xpath("/html/body/app-root/app-side-nav/mat-sidenav-container/mat-sidenav-content/main/app-otp/app-page-container/div/form/div/div/form/div/div[1]/button")).click();

HTML Button please click

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Rene Alano
  • 21
  • 5

5 Answers5

1

I hope these Three helps,

//button[@type='submit']
//*[contains(@type,'submit')]
//button[contains(text(),'VALIDATE')]

if Not try to post the HTML Code here so it will be helpful or, Check This Article try more on Yourself - https://www.guru99.com/xpath-selenium.html

koushick
  • 497
  • 2
  • 8
  • 30
1

find with multiple attribute use [@type=... and @class=...]

//button[@type='submit' and @class='mat-raised-button mat-primary']
ewwink
  • 18,382
  • 2
  • 44
  • 54
0

I think you are trying to automate website built on angular framework. In angular same div is used for multiple purpose. Developers just change binding and use the same button for multiple purpose.

If you share complete webpage html it would be a great help.

Just Check few things may be that help:

  1. Web element is visible on page.(css properties like display:none makes element disappear from page.It is still present in DOM but web driver can't find it)

  2. It is not loaded from jquery or ajax. if Yes, then Maybe you need to add webdriver wait for that.

Exact solution is depending on your complete web page.

Waqar Nadir
  • 378
  • 2
  • 9
0

First as a practice its better to use relative xpath, It will help you to reuse your code when new changes added.

Second if there are no ID , Name or any specific element locator to handle, Then you have to try for attribute like

`//button[contains(text(),'Validate')]`

`//button[@type='submit']`

//button[@aria-label='VALIDATE'] etc.

Devdun
  • 747
  • 5
  • 14
  • Hi @SDK_90 its not working Console: no such element: Unable to locate element: – Rene Alano Nov 08 '18 at 10:33
  • Hi @ReneAlano, There may be multiple reasons. delay time , element name is incorrect in script (case sensitive), Or actually element not visible within active window area but visible within page (you need to add scroll down). etc. First check locator name – Devdun Nov 12 '18 at 13:50
0

The desired element is an Angular element so to locate (and possibly invoke click()) you have to induce WebDriverWait for the desired element To Be Clickable and you can use either of the following solutions:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.mat-raised-button.mat-primary[aria-label='VALIDATE']>span.mat-button-wrapper"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='mat-raised-button mat-primary' and @aria-label='VALIDATE']/span[@class='mat-button-wrapper']"))).click();
    

Update

As you are facing the error ...unknown error: Element is not clickable at point... you can use either of the following solutions:

  • cssSelector:

    WebElement my_css_element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.mat-raised-button.mat-primary[aria-label='VALIDATE']>span.mat-button-wrapper")));
    ((JavascriptExecutor)driver).executeScript("arguments[0].click();", my_css_element);
    
  • xpath:

    WebElement my_xpath_element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='mat-raised-button mat-primary' and @aria-label='VALIDATE']/span[@class='mat-button-wrapper']")));
    ((JavascriptExecutor)driver).executeScript("arguments[0].click();", my_xpath_element);
    

Reference

You can find a related discussion in

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