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

  • 1
    Can you share the java code? – Joe Aug 28 '18 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();' – Harpreet Singh Aug 28 '18 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). – Harpreet Singh Aug 29 '18 at 0:03
  • 1
    What is the error message it is showing? Element not Visible? – Prasanna venkatesh Aug 29 '18 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? – Joe Aug 30 '18 at 5:09
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')]
  • 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 – Harpreet Singh Aug 29 '18 at 0:07
0

Try the below xpath,

//button[text()='CANCEL']
  • I tried driver.findElement(By.xpath("//button[text()='CANCEL']")).click(); but still throws an error. I have shared my code above – Harpreet Singh Aug 29 '18 at 0:05
  • @HarpreetSingh Can you try this XPath //form/following::button[text()='CANCEL'] – shanila Aug 29 '18 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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