0
@When("I click on sign in again")
    public void i_click_on_sign_in_again() {
    WebElement signin1  = driver.findElement(By.xpath("//a[@class='uitk-button uitk-button-medium uitk-button-fullWidth uitk-button-has-text uitk-button-primary']"));
        signin1.click();
    }

Xpath couldn't locate the element. What am I doing wrong?

enter image description here

1
  • Share the error message that you get while running this piece of code. That will help better to understand the issue. Otherwise the code: WebElement signin1 = driver.findElement(By.xpath("//a[@class='uitk-button uitk-button-medium uitk-button-fullWidth uitk-button-has-text uitk-button-primary']")); signin1.click(); is working just fine. I tried it by recreating the anchor tag in a local HTML page. Commented Aug 19, 2022 at 5:40

2 Answers 2

2

Above xpath not working because there a new line after button and 'primary' is in next line

1. if only and only want to find via class attribute then:

//a[contains(@class,'uitk-button uitk-button-medium uitk-button-fullWidth uitk—button-has-text uitk—button-')][contains(@class,'primary')]

OR

//a[contains(@class,'uitk-button uitk-button-medium uitk-button-fullWidth uitk—button-has-text uitk—button-')]

  1. Try finding with text:

xpath = "//a[contains(text(),'Sign in')]

OR

xpath = "//a[contains(@href,'/user/signin')]

1
  • 1
    The CSS doesn't have an actual newline character. That's just how the HTML will be displayed in the console or in an IDE with word wrapping. Commented Aug 19, 2022 at 5:39
0

Many companies provide automation testing services and this issue is very commonly faced by automation engineers. Below are the solutions to solve the problem:

Try finding with below xpath:

xpath = "//a[text()='Sign in']

Another way is through link text:

driver.findElement(By.linkText("Sign in"));

Sometimes if the page does not load properly then also element could not be located. For this scenario, you can add some wait.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.