简体   繁体   English

在赛普拉斯如何根据名称选择输入元素?

[英]In Cypress how to select input element based on name?

I'm starting to learn Cypress.我开始学习赛普拉斯。 I want to select the input field and provide the phone number by using cypress.io.我想使用 cypress.io 选择输入字段并提供电话号码。 The code I have following but it does not work.我遵循的代码但它不起作用。 However can I using find or there is another way to get the input element to type in phone number?但是我可以使用 find 还是有另一种方法可以让输入元素输入电话号码?

cy.get('div').contains('Phone Number').find('input[name=teacher[0].number]').type('8000-1612023')
<div class="required field">
  <label>Phone Number</label>
  <div title="Teacher number" class="ui fluid right labeled input no-spinners">
      <input required="" type="number" name="teacher[0].number" value="">
      <div class="ui label label">US</div>
  </div>
</div>

Why don't you directly target the input field using the following code为什么不直接使用以下代码定位输入字段

cy.get('input[name="teacher[0].number"]').type('8000-1612023')

Please find the screenshot below for a successful test.请在下面找到成功测试的屏幕截图。 I would also recommend you to change the type of input in your HTML to tel我还建议您将 HTML 中的输入类型更改为 tel

HTML: HTML:

<input required="" type="tel" name="teacher[0].number" value="">

Cypress:柏:

describe('Trial', function() {
  it('Test', function() {
    cy.visit('http://localhost:8080/trials/')
    cy.get('input[name="teacher[0].number"]').type('8000-1612023')
  })
});

Test Result:测试结果:

赛普拉斯结果的屏幕截图

Try this instead:试试这个:

cy.contains('div', 'Phone Number').find('input').first().type('8000-1612023')

The first argument to contains tells cypress to find the element with that selector that contains the text. contains的第一个参数告诉 cypress 使用包含文本的选择器查找元素。

I write selectors with the input tag as below and it worked for me.我使用如下输入标签编写选择器,它对我有用。

cy.get('input[name="elementName"]').type(val)

Check this to learn best practices when selecting elements:选中此项以了解选择元素时的最佳实践:

https://docs.cypress.io/guides/references/best-practices#Selecting-Elements https://docs.cypress.io/guides/references/best-practices#Selecting-Elements

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM