NoSuchElementException:没有这样的元素:无法找到元素错误使用Selenium和Java单击注册按钮
因此,我正在尝试使用java中的selenium测试此链接()的first name部分,以显示错误消息“first name只能包含字符”。当我在网站的first name部分键入“545”时,将显示该消息。但是,我的测试失败,这是我得到的错误:NoSuchElementException:没有这样的元素:无法找到元素错误使用Selenium和Java单击注册按钮,java,selenium,xpath,css-selectors,webdriverwait,Java,Selenium,Xpath,Css Selectors,Webdriverwait,因此,我正在尝试使用java中的selenium测试此链接()的first name部分,以显示错误消息“first name只能包含字符”。当我在网站的first name部分键入“545”时,将显示该消息。但是,我的测试失败,这是我得到的错误: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"*[name='submit']"}
(Session info: chrome=86.0.4240.198)
有人知道如何解决这个问题吗
import org.testng.AssertJUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class WebTesting {
@Test
public void test1() {
System.setProperty("webdriver.chrome.driver", "C:/Users/mtecl/Desktop/Drivers/chromedriver.exe"); // configure path to the driver
WebDriver driver = new ChromeDriver();
driver.get("https://softwaretestinggn8fe.herokuapp.com/");
//Username
WebElement testUsername = driver.findElement(By.id("firstname")); //id: first name
testUsername.sendKeys("545");
//Button
driver.findElement(By.name("submit")).click();
String expected = "First Name can only contain characters.";
String actual = driver.findElement(By.xpath("html/body/div/main/div[2]/form/div[1]/span")).getText();
Assert.assertEquals(expected, actual);
}
}
要单击元素注册上的()
,您需要为元素导入可选择的()
,您可以使用以下任一选项:
- CSS选择器:
- xpath:
- 控制台输出:
First Name can only contain characters.
- 浏览器快照:
工具书类 您可以在中找到关于以下内容的两个相关详细讨论:
driver.get("https://softwaretestinggn8fe.herokuapp.com/");
WebElement testUsername = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='firstname']")));
testUsername.sendKeys("545");
testUsername.submit();
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='firstname']//following-sibling::span[1]"))).getText());
First Name can only contain characters.