1

I am trying to click a button through webdriver. After inspecting the button element it highlights to the following:

Code Snippet

XPath:

/html/body/div/div/main/div/div[2]/div/div[2]/table/tbody/tr/td[4]/form/button

This is my code:

WebDriver driver= new FirefoxDriver(); driver.get("Website link");
driver.findElement(By.id("email")).sendKeys("xyz"); 
driver.findElement(By.id("password")).sendKeys("xyz"); 
driver.findElement(By.xpath("//*[text()='Log in']")).click(); 
driver.findElement(By.xpath("//button[text()='CANCEL']")).click();

But, when I try to run the code, it doesn't click the button. Can anyone help me solve this? Would be a great help

6
  • 1
    Can you share the java code?
    – Syrus
    Commented Aug 28, 2018 at 5:11
  • 'WebDriver driver= new FirefoxDriver(); driver.get("Website link"); driver.findElement(By.id("email")).sendKeys("xyz"); driver.findElement(By.id("password")).sendKeys("xyz"); driver.findElement(By.xpath("//*[text()='Log in']")).click(); driver.findElement(By.xpath("//button[text()='CANCEL']")).click();' Commented Aug 28, 2018 at 23:46
  • I tried all possible Xpath as mentioned in the comments but none of them works and would still throw an error message. Maybe am doing something wrong with the code. I have shared my code in the above comment, if anyone can let me know if there's something that needs to be changed, that'll be great. Also checked few other scenarios and am really struggling with accessing buttons which are under form(As per the screenshot attached above). Commented Aug 29, 2018 at 0:03
  • 1
    What is the error message it is showing? Element not Visible? Commented Aug 29, 2018 at 6:11
  • 2
    It's better if you add the code segment and error message to the question and update it. Could you click the login button? or this happens only for cancel button?
    – Syrus
    Commented Aug 30, 2018 at 5:09

7 Answers 7

0

Try this below Xpath

//button[contains(text(),'CANCEL')]
0

@Harpeet - Try this

//button[@class='btn btn-outline-primary bold']

if not works then try this.

//button[contains(text(),'CANCEL')]
1
  • I tried driver.findElement(By.xpath("//button[text()='CANCEL']")).click(); but still throws an error for some reason. I have attached my code above. Let me know if you need any more information Commented Aug 29, 2018 at 0:07
0

Try the below xpath,

//button[text()='CANCEL']
2
  • I tried driver.findElement(By.xpath("//button[text()='CANCEL']")).click(); but still throws an error. I have shared my code above Commented Aug 29, 2018 at 0:05
  • @HarpreetSingh Can you try this XPath //form/following::button[text()='CANCEL']
    – shanila
    Commented Aug 29, 2018 at 5:10
0

or you can use CSS selector

button.btn.btn-outline-primary.bold
0

For software testing services company while automating the application we avoid the usage of the Xpaths. We give preferences to the Classname>=Id>=Linktext>=Css and then last preference is given to the Xpath. Please try the code below.

public void clickCancelBtn() {    
webElement = driver.findElement(By.cssselector("button.btn.btn-outline-primary.bold"));
webElement.click(); 
}

Also, Form seems to be written using jquery. We have to look into the other solution if it not working at your end. Please do share your code snippet or error that it throws if it is not working at your end.

0

You can check whether there is a existence of a web element that obscures the web element that we want to click such as Frame.

Else you can add a wait type such as explicit or fluent wait before clicking the element. May be you are trying to click the element which is still not loaded in the UI.

1
  • Welcome to SQA stack exchange. Your answer would be much improved if you were to give examples of how the OP could check for an obscuring element, or how they could handle the wait.
    – Kate Paulk
    Commented May 19, 2023 at 14:30
0

Try using Explicit Wait in your code. Using the Explicit Wait command, the WebDriver is directed to wait until a specific condition occurs before executing the code.

WebDriver driver= new FirefoxDriver(); driver.get("Website link");
driver.findElement(By.id("email")).sendKeys("xyz"); 
driver.findElement(By.id("password")).sendKeys("xyz"); 
driver.findElement(By.xpath("//*[text()='Log in']")).click(); 
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[text()='CANCEL']")));
driver.findElement(By.xpath("//button[text()='CANCEL']")).click();

For more please refer article - https://www.browserstack.com/guide/wait-commands-in-selenium-webdriver#:~:text=Selenium%20for%20Free-,Explicit%20Wait%20in%20Selenium,-By%20using%20the

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.