I am trying to click a button through webdriver. After inspecting the button element it highlights to the following:
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
-
1Can you share the java code?Syrus– Syrus2018年08月28日 05:11:23 +00:00Commented 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();'Harpreet Singh– Harpreet Singh2018年08月28日 23:46:47 +00:00Commented 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).Harpreet Singh– Harpreet Singh2018年08月29日 00:03:28 +00:00Commented Aug 29, 2018 at 0:03
-
1What is the error message it is showing? Element not Visible?Prasanna venkatesh– Prasanna venkatesh2018年08月29日 06:11:26 +00:00Commented Aug 29, 2018 at 6:11
-
2It'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– Syrus2018年08月30日 05:09:02 +00:00Commented Aug 30, 2018 at 5:09
7 Answers 7
Try this below Xpath
//button[contains(text(),'CANCEL')]
@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 informationHarpreet Singh– Harpreet Singh2018年08月29日 00:07:52 +00:00Commented Aug 29, 2018 at 0:07
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 aboveHarpreet Singh– Harpreet Singh2018年08月29日 00:05:00 +00:00Commented Aug 29, 2018 at 0:05 -
@HarpreetSingh Can you try this XPath //form/following::button[text()='CANCEL']shanila– shanila2018年08月29日 05:10:36 +00:00Commented Aug 29, 2018 at 5:10
or you can use CSS selector
button.btn.btn-outline-primary.bold
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.
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.
-
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– Kate Paulk2023年05月19日 14:30:55 +00:00Commented May 19, 2023 at 14:30
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
Explore related questions
See similar questions with these tags.