【问题标题】:Cannot change text in input field - input element is not found, but it exists无法更改输入字段中的文本 - 未找到输入元素,但它存在
【发布时间】:2018-08-22 02:44:51
【问题描述】:

我正在使用 Selenium 测试 Web 服务。页面上的某些元素是由 Angular 修改的。我正在尝试更改输入字段中的文本,但是当我检查源代码时,它不是 input,而是不可编辑的 span 元素。

例如,在https://www.w3schools.com/angular/tryit.asp?filename=try_ng_form(不是我正在测试的页面,但显示完全相同的问题)上,我们有一些输入元素:

<form novalidate="" class="ng-pristine ng-valid">
    First Name:<br>
    <input type="text" ng-model="user.firstName" class="ng-pristine ng-valid ng-not-empty ng-touched"><br>
    Last Name:<br>
    <input type="text" ng-model="user.lastName" class="ng-pristine ng-untouched ng-valid ng-not-empty">
    <br><br>
    <button ng-click="reset()">RESET</button>
</form>

为这样的元素编写 xpath 并不难,但令我惊讶的是 Selenium 在页面上找不到任何 inputs

ValueError: Element locator 'xpath=//input' did not match any elements.

所以我使用 ExtendedSelenium2Library 中的 Log Source 关键字来验证它,在记录的源代码中,我感兴趣的 input 元素看起来像这样:

<span class="cm-m-xml cm-tag">input</span>

所以这实际上是一个span

首先,我认为 Angular 与这种行为有一些共同点,我尝试通过一些特定的 Angular 定位器来定位元素,例如:

input text  model=user.firstName  Hello!

它总是以

结束
ValueError: Element locator 'model=user.firstName' did not match any elements

所以我为span 元素编写了xpath。找到了,但最后是:

InvalidElementStateException: Message: invalid element state: Element must be user-editable in order to clear it.

我最后的代码是这样的(这只是测试代码):

*** Settings ***
#Library         Selenium2Library
#Library         AngularJSLibrary
Library          ExtendedSelenium2Library
Test Setup  open browser  url=${url}  browser=chrome
Test Teardown  close browser


*** Variables ***
${url}  https://www.w3schools.com/angular/tryit.asp?filename=try_ng_form


*** Test Cases ***
Will change it?
  #  Go to  ${url}
  #  Wait for angular
  #  input text  model=user.firstName  Hello!
     Log Source
     input text  xpath=//span[@class="cm-m-javascript cm-string" and text()='"John"']  Hello!

我尝试了 Selenium2Library、AngularJSLibrary、ExtendedSelenium2Library 和很多 xpath 表达式,但没有成功。

为了使长问题简短:基于此example page, 如何使用 Robot Framework Selenium 更改名字输入字段的值?

我的pip freeze的一部分:

robotframework==3.0.2
robotframework-angularjs==0.0.6
robotframework-extendedselenium2library==0.9.1
robotframework-selenium2library==1.8.0

我使用的是 Python 2.7.13。

【问题讨论】:

  • 在链接的示例页面上,//input 返回六个匹配项,//input[@ng-model='user.firstName'] 匹配第一个名称输入元素。
  • @BillHileman 您能否与我分享您正在使用的库版本?也许我有一些版本问题。
  • 我只使用 Chrome 开发者工具,抱歉。我只是验证 xpath 访问,仅此而已。
  • 这是正确的,当我在 web 控制台中使用 //input xpath 解析页面源代码时,它显示了 6 个元素,但使用 ExtendedSelenium2Library 它不匹配任何元素。
  • 那么我认为您走在正确的道路上,因为它几乎必须是硒库问题。我不使用 angular 或 python,所以恐怕我不能再提供帮助了。

标签: angular selenium robotframework


【解决方案1】:

感谢@Ian 的评论和我的经理在工作中的帮助,我终于解决了这个问题。

首先我不知道 iframe 如此特别。如果有人想读一些关于他们的东西here 是好文章。

所以我尝试使用 Select Frame 关键字选择合适的框架,但我在 chrome 中选择它时遇到了问题,结果是

NoSuchFrameException: Message: no such frame: element is not a frame

但我发现我使用的 chrome 有一些 issues 具有正确的帧检测功能。

所以我尝试使用 FF,结果如下:对于 FF 版本 58,我在浏览器打开期间出现超时异常

Setup failed:
WebDriverException: Message: timeouts

所以我将其降级为 FF 版本 55,并在 Select Frame 调用时出错

NoSuchFrameException: Message: Unable to locate frame

所以我再次将其降级到版本 53 并在发送密钥时遇到问题:

WebDriverException: Message: Expected [object Undefined] undefined

link 进行描述。最后将其降级到版本 52,下面的代码开始工作:

*** Settings ***
Library          SeleniumLibrary
Test Setup  open browser  ${url}
Test Teardown  close browser


*** Variables ***
${url}  https://www.w3schools.com/angular/tryit.asp?filename=try_ng_form


*** Test Cases ***
Will change it?
    Log Source  # for debug
    Select frame  iframeResult  
    Log Source  # for debug
    Input Text   xpath=//input  Hello!

我还发现我使用的 Selenium2Library 是旧版,并将其更改为 SeleniumLibrary

这是我的最终库版本:

robotframework==3.0.2
robotframework-angularjs==0.0.6
robotframework-extendedselenium2library==0.9.1
robotframework-selenium2library==1.8.0
robotframework-seleniumlibrary==3.1.1

最后,我将我的 chrome 升级到版本 64.0.3282.186(我可能应该从它开始)并且上面显示的代码完美运行。对于目标 Web 服务也是如此;)

感谢所有尝试帮助的人!

【讨论】:

    猜你喜欢
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多