1

I have an element which selenium picks up as disabled. It is however, enabled. As such I click on it using javascript. Below is the snippet I use:

IWebElement button = driver.FindElement(By.XPath(ButtonXpath));
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click()", button);

When I exeucte this code, nothing happens. It steps over these lines, and the button (anchor actually in this case) doesnt get clicked. I cant use id as the anchor has none.

What should I do to enforce a click?

Thanks, Rahul

0

1 Answer 1

1

This is happening most probably because of the wrong selector or element load issue. Before executing the script you need to make sure the element was found correctly. Check the selector and use some explicit wait if needed.

//This test works with or without the explicit wait
[Test]
public void ExecutorTest()
{
    _webDriver = new ChromeDriver();
    _webDriver.Navigate().GoToUrl("https://github.com/");
    _webDriver.Manage().Window.Maximize();

    By xpathBy = By.XPath("//a[@href='/join']");
    IWebElement element =
        new WebDriverWait(_webDriver, TimeSpan.FromSeconds(10))
        .Until(ExpectedConditions.ElementExists(xpathBy));

    ((IJavaScriptExecutor)_webDriver).ExecuteScript("arguments[0].click();", element);

    _webDriver.Quit();

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.