【问题标题】:Selenium: finding the right element identifierSelenium:找到正确的元素标识符
【发布时间】:2019-04-24 05:48:47
【问题描述】:

我对 selenium 自动化比较陌生,正在尝试测试为我创建的表单。我想将测试值放入 HTML 代码如下所示的文本框中:

<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"}

老实说,我不知道我在哪里出错了 - 在此先感谢您的帮助。

是否有任何“列出所有元素 id”功能,以便我可以将它们与 chrome 检查器视图中显示的内容结合起来?

【问题讨论】:

  • 请提供您尝试通过其 ID 获取元素的整行代码,(...) 没有提供足够的信息。
  • 对不起,这只是 chromedriver 对象 - 我现在扩展了示例代码
  • 您是否可能在页面完全加载之前尝试获取元素?您可能想阅读这篇关于如何使用 webdriver 等待的 SO 帖子 (stackoverflow.com/questions/13244225/…)
  • 不,我正在调试器中单步执行代码,它甚至不允许我在页面完成“导航”行并完全加载之前进入该行 - 或在至少它在视觉上看起来是满载的,如果我在那里按 F12,铬窗口可以看到元素。
  • @MartingKS 是你想要包裹在框架内的元素,iframe?

标签: vb.net selenium selenium-chromedriver


【解决方案1】:

由于您的元素位于框架内,您需要先将驱动程序导航到框架,框架有自己的文档,其中包含 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/)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 2020-02-17
    相关资源
    最近更新 更多