我对selenium自动化比较陌生,正在尝试测试为我创建的表单。我想将测试值放入一个文本框中,其超文本标记语言代码如下所示:
<input type="text" id="txtEndCustId" onkeypress="return isNumberKey(event);" class="Text" required="" onfocus="txtEndCustIdFocus('Your customer END CUSTOMER DEP ID');" onblur="txtEndCustIdblur();">
我想我会使用盒子的“id”,但是代码:
Dim options As New Chrome.ChromeOptions
Dim service As ChromeDriverService = ChromeDriverService.CreateDefaultService
Dim wd As New Chrome.ChromeDriver(service, options)
wd.Navigate.GoToUrl("x")
wd.FindElementById("txtEndCustId").SendKeys("1")
给出一个错误:
Unable to locate element: {"method":"id","selector":"txtEndCustId"}
老实说,我不知道我在这里做错了什么——提前感谢你的帮助。
是否有任何“列出所有元素”功能,以便我可以将它们与chrome检查器视图中显示的内容结合起来?
因为你的元素在框架内,你需要先将驱动程序导航到框架,框架有它自己的文档,包含x个DOM元素。
首先切换到框架:
//You need to use method switchTo() to set the driver to the frame document
//In this case we are passing in the id of the iframe
wd.switchTo().frame("ctl00_CPH_iframeCat");
//Now that the driver is working on the frame document, you should be able to manipulate the
//input you want
wd.FindElementById("txtEndCustId").SendKeys("1")
//Finally, switch the driver back to the main page document (original document which contains the frames)
driver.switchTo().defaultContent();
这是一个很好的留档,应该更详细地解释这一点:(http://www.assertselenium.com/webdriver/handling-iframes-using-webdriver/)