赛普拉斯:是否可以从只有部分单词/s的动态下拉列表中选择一个项目?

发布于 2025-02-08 10:05:37 字数 399 浏览 1 评论 0原文

我有一个下拉列表,该列表总是在可用的选项数量增加。我从选项中获得了“ recieptName”,但是,随后是不断变化的文本。例如:recieptname:“更改句子,包括单词和数字”。

我要做的是类似的事情:

cy.get('#RecieptName').select('RecieptName:');

但是,它找不到一个选项,因为它随后更改数字。是否可以根据部分选项找到该选项?

I have a dropdown list which is always increasing in the number of options available. I Have the 'RecieptName' from the option, however, it is followed by text that is constantly changing. eg: RecieptName: 'Changing sentence including words and numbers'.

What I am trying to do is something along the lines of:

cy.get('#RecieptName').select('RecieptName:');

However, it can't find an option with it as it is followed by changing the numbers. Is it possible to find the option based on a partial option?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

快乐很简单 2025-02-15 10:05:37

您需要首先扫描选项,找到索引,然后

使用.contains()命令

cy.get('option')
  .contains('ReceiptName:')
  .invoke('index')           // index among siblings
  .then(index => {
    cy.get('dropdown').select(index);
  })

使用正则

cy.get('option')
  .contains(/^ReceiptName:/)
  .invoke('index')           // index among siblings
  .then(index => {
    cy.get('dropdown').select(index);
  })

>

cy.get('#RecieptName option')
  .contains(/^ReceiptName:/)
  .invoke('prop', 'selected', true)

You would need to scan the options first, find it's index and select by number

Using .contains() command

cy.get('option')
  .contains('ReceiptName:')
  .invoke('index')           // index among siblings
  .then(index => {
    cy.get('dropdown').select(index);
  })

Using a regex

cy.get('option')
  .contains(/^ReceiptName:/)
  .invoke('index')           // index among siblings
  .then(index => {
    cy.get('dropdown').select(index);
  })

Using selected prop

cy.get('#RecieptName option')
  .contains(/^ReceiptName:/)
  .invoke('prop', 'selected', true)
故事与诗 2025-02-15 10:05:37

包含文本“ recieptName:”的选项可以通过添加:contains()来选择

cy.get('#RecieptName option:contains(RecieptName:)')
  .then($option => {
    cy.get('#RecieptName').select($option.text())
  })
cy.get('#RecieptName option:contains(RecieptName:)')
  .then($option => {
    cy.get('#RecieptName').select($option.index())
  })

The option containing the the text "RecieptName:" can be selected by adding :contains()

cy.get('#RecieptName option:contains(RecieptName:)')
  .then($option => {
    cy.get('#RecieptName').select($option.text())
  })
cy.get('#RecieptName option:contains(RecieptName:)')
  .then($option => {
    cy.get('#RecieptName').select($option.index())
  })
吻风 2025-02-15 10:05:37

过滤器将获得与特定选择器匹配的DOM元素。获得元素后,我们通过使用Invoke(text)获得 recieptName 选项的确切文本,然后我们将相同的文本传递给select select < /code>命令,类似的内容:

cy.get('#RecieptName option')
  .filter(':contains("RecieptName:")')
  .invoke('text')
  .then((recieptNameText) => {
    cy.get('dropdown').select(recieptNameText)
  })

您还可以使用value属性直接选择下拉列表,因为我可以看到值属性是唯一的,我想这将是最清洁的方法:

cy.get('select#RecieptName').select('scoredesc')
cy.get('select#RecieptName').select('modifieddesc')
cy.get('select#RecieptName').select('createdasc')

filter will get the DOM element that match a specific selector. Once we get the element, then we get the exact text of RecieptName option by using the invoke(text) and then we pass the same text to the select command, something like this:

cy.get('#RecieptName option')
  .filter(':contains("RecieptName:")')
  .invoke('text')
  .then((recieptNameText) => {
    cy.get('dropdown').select(recieptNameText)
  })

You can also use the value attribute to directly select the dropdown as I can see the value attributes are unique, I guess that would be the cleanest way to do this:

cy.get('select#RecieptName').select('scoredesc')
cy.get('select#RecieptName').select('modifieddesc')
cy.get('select#RecieptName').select('createdasc')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文