NoSuchElementException:没有这样的元素:无法使用Selenium和Java找到元素名称/ id属性

问题描述 投票:0回答:1

im试图用Selenium Java通过名称或id来定位元素,但我无法找到

System.setProperty("webdriver.chrome.driver",driverPath + "chromedriver");
System.out.println(driverPath + "chromedriver");
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, 15);
String baseUrl = "xxxx";
driver.get(baseUrl);
driver.manage().window().maximize();
ByName salutation = new ByName("salutation");
wait.until(ExpectedConditions.elementToBeClickable(salutation));
WebElement root1 = driver.findElement(salutation);

这就是我得到的信息。即时消息超级困惑为什么我收到此错误消息,提示“方法:css选择器”,因为我显然没有使用css选择器:

Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.name: salutation (tried for 15 second(s) with 500 milliseconds interval)
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
    at de.xx.tests.XXTests.main(XXTests.java:29)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"*[name='salutation']"}
  (Session info: chrome=78.0.3904.108)
selenium selenium-webdriver xpath css-selectors webdriverwait
1个回答
0
投票

[ByName是由WebDriver定义的API,实际实现将其转换为CSS选择器*[name='salutation']

如您所见,它正在尝试通过名称查找元素,但无法找到。

没有要测试的页面的HTML代码,就无法说出为什么找不到它:鉴于您使用了elementToBeClickable,该元素必须是可见的且已启用,以便您可以单击它

© www.soinside.com 2019 - 2024. All rights reserved.