简体   繁体   English

如何在不使用变量定位的情况下找到此元素?

[英]How can I locate this element without using the variant to locate it?

I am trying to use selenium to locate this element and click this element, but the variant changes every time. 我正在尝试使用selenium来定位此元素并单击此元素,但变体每次都会更改。 I need selenium to click this button, "M". 我需要selenium点击这个按钮,“M”。 How can I locate this element without using "33246615686" 如何在不使用“33246615686”的情况下找到此元素

<label class="radio-label " for="variant-select-33246615686">M</label>

尝试以下XPath

//label[text()="M"]

您可以使用文本“M”通过xpath查找它:

//label[contains(text(),"M")]

The answers above will work but it is a recipe for fragile code. 上面的答案将起作用,但它是脆弱代码的一个秘诀。 I avoid using label text as much as possible. 我尽量避免使用标签文本。 A better locator will be: 更好的定位器将是:

//label[starts-with(@for, 'variant-select')]

or 要么

//label[contains(@for,'variant-select')]

Also, if you will be using this in a localization test for example, it will fail because the text will be written in the locale language the application is set to at the time of testing. 此外,如果您将在本地化测试中使用它,例如,它将失败,因为文本将使用应用程序在测试时设置的语言环境语言编写。 For example, the M in Chinese will be hence, the text() will not see this text. 例如,中文中的M将为因此text()将不会看到此文本。 Considering localization in your application ? 考虑在您的应用程序中本地化? This is better. 这个更好。

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

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