【发布时间】:2018-10-23 15:04:27
【问题描述】:
我使用 css 来定位元素,我试图避免使用 xpath 以防万一,但我遇到了元素问题。我仍然收到错误:
(node:7516) UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to locate element: {"method":"css selector","selector":"[name-firstName]"}
它只适用于 xpath,但我想使用 css 定位器。定位元素时避免使用 xpath 是一种好习惯吗?谢谢你们!
这是整个元素:
<input type="text" class="f w-input ng-pristine ng-invalid ng-invalid-required ng-touched" name="firstName" required="" ng-readonly="vm.showLoading" ng-model="vm.formData.firstName" placeholder="Your First Name">
这里是代码试用:
driver.get("www.examplewebsiteonly.com")
.then(function() {
return driver.wait(until.elementLocated(By.css('[name=firstName]')), 20000)
.then(function(){
driver.sleep(20000)
.then(function(){
return driver.findElement(By.css('[name-firstName]')).sendKeys("FirstName");
})
})
})
【问题讨论】:
标签: selenium selenium-webdriver xpath css-selectors webdriver