javascript error: a.tagName.toUpperCase is not a function error clicking on a button using Selenium and Java

javascript error: a.tagName.toUpperCase is not a function error clicking on a button using Selenium and Java

我正在使用带有 testng 的 Selenium Webdriver。在其中一个 WebElements 上,我试图执行单击按钮操作。 我正在选择定位器“ID”并执行以下操作。

WebDriver webDriver = this.getWebDriver();
webDriver.findElement(By.id("addTagBtn")).click();

我遇到了以下错误 -

javascript error: a.tagName.toUpperCase is not a function error

HTML 代码如下所示

这个错误信息...

javascript error: a.tagName.toUpperCase is not a function error

...表示 instance attempted to invoke click() on the desired element even before the element was completely with in the DOM Tree.


所需的元素是 Angular element. So to click() on the element you need to induce for the elementToBeClickable() and you can use either of the following :

  • id:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("addTagBtn"))).click();
    
  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("td > button#addTagBtn"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//td/button[@id='addTagBtn' and text()='Add']"))).click();
    

当我使用 Javascript 代码即

时它起作用了
JavascriptExecutor js = (JavascriptExecutor) webDriver;  
        js.executeScript("document.getElementById('addTagBtn').click();");
    ```