Selenium 点击这个按钮

Selenium to click this button

我正在尝试为 select 这个按钮创建一个测试

<button type="submit" class="btn btn--stretch btn--primary btn--green t-auth__login--btn">
                Create my account</button>

我用的代码是这样的,但是不行

driver.findElement(By.className("btn btn--stretch btn--primary btn--green t-auth__login--btn")).click();

有人能告诉我这个测试的正确编码方式吗?

如果有帮助,相关网页是https://www.dunnesstores.com/customer/login

click() 元素上您可以使用以下任一项 :

  • cssSelector:

    driver.findElement(By.cssSelector("button.btn.btn--stretch.btn--primary.btn--green.t-auth__login--btn")).click();
    
  • xpath:

    driver.findElement(By.xpath("//button[@class='btn btn--stretch btn--primary btn--green t-auth__login--btn' and contains(., 'Create my account')]")).click();
    

理想情况下 click() 在您需要为 elementToBeClickable() 引入 的元素上,您可以使用以下任一方法 :

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.btn.btn--stretch.btn--primary.btn--green.t-auth__login--btn"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='btn btn--stretch btn--primary btn--green t-auth__login--btn' and contains(., 'Create my account')]"))).click();