-1

I have tried to click a button using id, then class name, then xpath, id is given dynamically. Could you please tell me the exact xpath for this code

package step_definitions;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

 public class sharedshelf {
  public WebDriver driver;
    public sharedshelf()
    {
        driver = Hooks.driver;
    }
 @When("^I press  option button$")
    public void i_press_option_buttion() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        Thread.sleep(5000);
        driver.findElement(By.xpath("//button[text()='Edit']")).click();
    }

Html

<button type="button" id="ext-gen494" class=" x-btn-text" tabindex="4">Edit</button>
Guy
  • 46,488
  • 10
  • 44
  • 88
ali
  • 9
  • 1
  • 3

2 Answers2

0

The button is not clickable when you are trying to click on it. Try waiting for the button to be clickable.

You can also locate the button by partial id using cssSelector By.cssSelector("[id*='ext']")

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("[id*='ext']")));
button.click();
Guy
  • 46,488
  • 10
  • 44
  • 88
0

An exception might be thrown when clicking on element in these cases: If element is disabled and selenium tries to click - it does not automatically wait until it is clickable Also it can happen if element not visible / element not clickable (hidden under another element) the solution of waiting until element clickable (under the hood it waits until visible and enabled) will not solve Stale Element Reference Exception http://docs.seleniumhq.org/exceptions/stale_element_reference.jsp

The best way is to create a method like this:

    public void TryClick(By by)
    {
        DateTime timeout = DateTime.Now.AddSeconds(10);
        bool clickedSuccessfully = false;
        Exception exceptionWhileClick = null;
        do
        {
            try
            {
                WrappedElement.FindElement(by).Click();
                clickedSuccessfully = true;
            }
            catch (Exception e)
            {
                // ignored
                exceptionWhileClick = e;
            }
        } while (!clickedSuccessfully && DateTime.Now < timeout);

        if (!clickedSuccessfully && exceptionWhileClick != null)
            throw exceptionWhileClick;

    }
Liraz Shay
  • 86
  • 4