Rene Alano
Rene Alano

Reputation: 21

How to locate a complicated tag without id name or value?

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

Upvotes: 1

Views: 1182

Answers (5)

undetected Selenium
undetected Selenium

Reputation: 193338

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

Upvotes: 0

Devdun
Devdun

Reputation: 767

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.

Upvotes: 0

Waqar Nadir
Waqar Nadir

Reputation: 388

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.

Upvotes: 0

ewwink
ewwink

Reputation: 19164

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

//button[@type='submit' and @class='mat-raised-button mat-primary']

Upvotes: 1

koushick
koushick

Reputation: 497

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

Upvotes: 1

Related Questions