当我试图找到一个元素时没有这样的元素异常

时间:2021-04-26 11:13:37

标签: javascript java selenium-webdriver

我正在尝试使用下面的 selenium webdriver 来识别名字字段,但没有元素异常:

元素尝试:

"<input onkeyup="$('#lastname').val(this.value);" type="text" class="is_required validate form-control" data-validate="isName" id="customer_lastname" name="customer_lastname" value="">"

使用的xpath: //输入[@name='customer_lastname']

出现以下错误: org.openqa.selenium.NoSuchElementException:没有这样的元素:无法定位元素:{"method":"xpath","selector":"//input[@name='customer_lastname']"} (会话信息:chrome=89.0.4389.128) 有关此错误的文档,请访问:http://seleniumhq.org/exceptions/no_such_element.html 构建信息:版本:'3.11.0',修订版:'e59cfb3',时间:'2018-03-11T20:26:55.152Z' 系统信息:主机:'HEMANTH-PC',ip:'192.168.2.5',os.name:'Windows 7',os.arch:'amd64',os.version:'6.1',java.version:'1.8 .0_131' 驱动程序信息:org.openqa.selenium.chrome.ChromeDriver 功能 {acceptInsecureCerts: false, browserName: chrome, browserVersion: 89.0.4389.128, chrome: {chromedriverVersion: 89.0.4389.23 (61b08ee2c5002..., userDataDir: C:\Users\Hemanth\AppData\Lo...}, goog : {debuggerAddress: localhost:56336}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, 脚本: 30000}, unhandledPromptBehavior: 关闭和通知, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true} 会话 ID:ecc4ae51b43447b6f4f10573bb13424d *** 元素信息:{Using=xpath, value=//input[@name='customer_lastname']} 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 在 java.lang.reflect.Constructor.newInstance(Constructor.java:423) 在 org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187) 在 org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122) 在 org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49) 在 org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158) 在 org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83) 在 org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:545) 在 org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:319) 在 org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:421) 在 org.openqa.selenium.By$ByXPath.findElement(By.java:361) 在 org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:311) 在 com.pages.LandingPage.enterlastName(LandingPage.java:36) 在 stepdefinitions.Stepdefinitions.user_enters_and_click_on_create_account_button(Stepdefinitions.java:73) 在✽.user 输入“hemanth123@gmail.com”并单击创建帐户按钮(file:///E:/​​APITESTINGWorkspace/AutomationPractice/src/test/java/com/features/RegistrationandShopping.feature:6)

1 个答案:

答案 0 :(得分:0)

1)转到您的浏览器 -> 手动打开应用程序

2)打开检查器窗口(即按键盘上的功能键 f12)

3)转到控制台选项卡,输入如下内容并点击回车:

$x("//input[@name='customer_lastname']");

4)如果在控制台中标识了元素并且计数为 1,但在 Selenium 中您得到 NoSuchElementException,那么您尝试定位的元素很有可能在 Iframe 内。如果是这样,找出Iframe并切换到它,然后尝试调用findElement方法,它应该可以工作。

相关问题