【问题标题】:Using selenium webdriver for automation but Not able to click on element使用 selenium webdriver 进行自动化但无法单击元素
【发布时间】:2023-03-19 13:57:01
【问题描述】:

我正在使用 selenium webdriver 在我的网站上执行自动化。我可以登录网站但无法对元素执行点击操作。 我的代码尝试是:

WebElement add = BrowserUtilities.driver.findElement(By.xpath("//button[@class = 'btn btn-primary btn-lg']"));
add.click();

我也尝试过使用 javascript 执行器,如下所示:

JavascriptExecutor js = (JavascriptExecutor) BrowserUtilities.driver;
js.executeScript("argument[0].click()", add);

现在我在控制台中遇到异常,例如:

FAILED CONFIGURATION: @BeforeClass launchBrowserTest
org.openqa.selenium.WebDriverException: unknown error: argument is not defined

如果有其他解决方案,请建议我。

【问题讨论】:

  • 用参数[0]替换参数[0]
  • 忽略上面的评论。你在争论后错过了。用参数[0]替换参数[0]

标签: java selenium


【解决方案1】:

这是因为存在 javascript 和 ajax 调用,您可以尝试以下代码:

使用 webdriver 查找元素等待:

 WebDriverWait wait=new WebDriverWait(driver,50 );           
    WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//button[@type='Cancel']")));

然后使用 Actions 类执行点击操作:

Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perfrom();

【讨论】:

    【解决方案2】:

    试试看。

     WebDriverWait wait = new WebDriverWait(driver,9000);   
     WebElement button=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class = 'btn btn-primary btn-lg']")));
        button.click();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      • 2023-03-10
      • 1970-01-01
      • 2020-11-10
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多