css - 我们如何选择下拉箭头键的 css 定位器-

标签 css selenium-webdriver

SCREENSHOT OF THE PAGE I NEED TO AUTOMATE

我有 3 个下拉列表,它们不是选择类。所以我需要点击每个元素的向下箭头键。这些箭头键的样式相同: .Select-arrow(使用的火路)

HTML:

<form accept-charset="UTF-8" autocomplete="off"><div class="form-group"><label for="nickname" class="control-label label__floating control-label">Nickname</label><input name="nickname" value="" id="nickname" class="signup-nickname form-control" type="text"><span class="help-block">Ex. LarrySaint, LSJ, LSSaintJohn, LSJohn</span></div><div class="form-group"><label class="control-label label__floating">Job Title</label><div class="Select Select--single"><div class="Select-control"><span class="Select-multi-value-wrapper" id="react-select-2--value"><div role="combobox" aria-expanded="false" aria-owns="react-select-2--value" aria-activedescendant="react-select-2--value" class="Select-input" tabindex="0" aria-readonly="false" style="border: 0px none; width: 1px; display: inline-block;"></div></span><span class="Select-arrow-zone"><span class="Select-arrow"></span></span></div></div></div><div class="form-container__phone"><div class="form-container__phone--country"><div class="form-group"><label class="control-label label__floating">Country Code</label><div class="Select Select--single is-searchable"><div class="Select-control"><span class="Select-multi-value-wrapper" id="react-select-3--value"><div role="combobox" aria-expanded="false" aria-owns="react-select-3--value" aria-activedescendant="react-select-3--value" class="Select-input" tabindex="0" aria-readonly="false" style="border: 0px none; width: 1px; display: inline-block;"></div></span><span class="Select-arrow-zone"><span class="Select-arrow"></span></span></div></div></div></div><div class="form-container__phone--number"><div class="form-group"><label for="phoneNumber" class="control-label label__floating control-label">Phone Number</label><input disabled="" name="phoneNumber" value="" id="phoneNumber" class="signup-phonenumber form-control" type="text"><span class="help-block">Ex. 855-434-7339, (510) 299 1234</span></div></div></div><div id="timezone"><div class="form-group"><label class="control-label label__floating">Select Timezone</label><div class="Select Select--single is-searchable"><div class="Select-control"><span class="Select-multi-value-wrapper" id="react-select-4--value"><div role="combobox" aria-expanded="false" aria-owns="react-select-4--value" aria-activedescendant="react-select-4--value" class="Select-input" tabindex="0" aria-readonly="false" style="border: 0px none; width: 1px; display: inline-block;"></div></span><span class="Select-arrow-zone"><span class="Select-arrow"></span></span></div></div></div></div></form>

因此,我无法唯一标识每个下拉箭头的 css 值。请帮忙鉴定。

在 ff 控制台中,

$$('.Select-arrow')


Array [ <span.Select-arrow>, <span.Select-arrow>, <span.Select-arrow> ]

我可以通过索引选择这些元素中的任何一个吗?请帮帮我。

尝试:

尝试使用这些 xpaths:

${NEW_USER_LOGIN_PAGE_JOB_TITLE_DROPDOWN}    //*[@id='react-select-2--value-item']/input
${NEW_USER_LOGIN_PAGE_JOB_TITLE_DROPDOWN_OPTION4}     //*[@id='react-select-2--option-4']
${NEW_USER_LOGIN_PAGE_COUNTRY_CODE_DROPDOWN}    //*[@id='react-select-3--value-item']/input
${NEW_USER_LOGIN_PAGE_COUNTRY_CODE_DROPDOWN_OPTION96}    //*[@id='react-select-3--option-96']
${NEW_USER_LOGIN_PAGE_TIME_ZONE_DROPDOWN}       //*[@id='react-select-4--value-item']/input
${NEW_USER_LOGIN_PAGE_TIME_ZONE_DROPDOWN_OPTION102}      //*[@id='react-select-4--option-102']

这些都不起作用。

最佳答案

您可以使用 XPath 或 CSS 选择器来查找它们。在这种情况下,我推荐 XPath,因为它更灵活,也更宽容。

第一个选项:使用 XPath 通过标签元素中的文本查找每个箭头的根元素。

NEW_USER_LOGIN_PAGE_JOB_TITLE_DROPDOWN: "//label[contains(text(),'Job Title')]/..//span[@class='Select-arrow']"
NEW_USER_LOGIN_PAGE_COUNTRY_CODE_DROPDOWN: "//label[contains(text(),'Country Code')]/..//span[@class='Select-arrow']"
NEW_USER_LOGIN_PAGE_TIME_ZONE_DROPDOWN: "//label[contains(text(),'Select Timezone')]/..//span[@class='Select-arrow']"

第二个选项:将 CSS 选择器 与特定 ID、类名、可搜索或顺序一起使用。

NEW_USER_LOGIN_PAGE_JOB_TITLE_DROPDOWN: "div.Select:not(.is-searchable) span.Select-arrow" //supposing the Job Title dropdown is not searchable.
NEW_USER_LOGIN_PAGE_JOB_TITLE_DROPDOWN: "form > div:nth-child(2) span.Select-arrow" //supposing the Job Title dropdown is always the second item in the form.
NEW_USER_LOGIN_PAGE_COUNTRY_CODE_DROPDOWN: "div[class*='__phone--country'] span.Select-arrow"
NEW_USER_LOGIN_PAGE_TIME_ZONE_DROPDOWN: "#timezone span.Select-arrow"

关于css - 我们如何选择下拉箭头键的 css 定位器-,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46538431/

相关文章:

css - 以句点开始 LESS 变量值

javascript - 自定义 jQuery 子菜单定位

selenium-webdriver - 如何防止每次启动新方法时 py.test 运行新的 webdriver

java - 如何使用 selenium webdriver java 测试或验证复选框是否被选中

python - 使用 "OR"组合多个 Selenium 等待?

html - 如何在所有浏览器中添加表格边框并使表格居中?

css - JavaFX - 用于定义 StageStyle 的 css 文件位于何处或如何更改它?

html - 线性渐变不同颜色的位置

java - Selenium:打开扩展的弹出窗口

java - 如何使用 Webdriver (Java) 获取下一个随机生成的父 ID