Selenium - 使用javascript单击按钮不起作用

Selenium - click on button using javasacript not working

我有一个 selenium 拾取为禁用的元素。但是,它已启用。 因此,我使用 javascript 单击它。 下面是我使用的片段:

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

当我执行这段代码时,没有任何反应。它越过这些线,并且按钮(在这种情况下实际上是锚点)没有被点击。 我不能使用 id,因为锚有 none.

我应该怎么做才能强制点击?

谢谢, 拉胡尔

发生这种情况很可能是因为错误的选择器或元素加载问题。在执行脚本之前,您需要确保已正确找到该元素。检查选择器并在需要时使用一些显式等待。

//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();

}