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

如何选择下拉箭头键的css定位器-,css,selenium-webdriver,Css,Selenium Webdriver,我有3个下拉列表,它们不是选择类。因此,我需要单击每个元素的向下箭头键。这些箭头键的css相同: .选择箭头(用于壁炉) HTML: 这些都不起作用。您可以使用XPath或CSS选择器来查找它们。在这种情况下,我建议使用XPath,因为它更灵活、更宽容 第一个选项:使用XPath通过label元素中的文本查找每个箭头的根元素 NEW_USER_LOGIN_PAGE_JOB_TITLE_DROPDOWN: "//label[contains(text(),'Job Title')]/..//spa

我有3个下拉列表,它们不是选择类。因此,我需要单击每个元素的向下箭头键。这些箭头键的css相同: .选择箭头(用于壁炉)

HTML:


这些都不起作用。

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

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

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']"
第二个选项:使用带有特定id、类名、可搜索或顺序的CSS选择器

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"

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

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

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']"
第二个选项:使用带有特定id、类名、可搜索或顺序的CSS选择器

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"

是的,您可以使用
driver.findElements
(注意“s”)按索引选择它们。这将返回定位器找到的WebElement列表。然后,您可以将该列表作为任何列表进行迭代。例如:

List<WebElement> arrows = driver.findElements(By.className("Select-arrow"));
WebElement jobTitle = arrows.get(0);  
WebElement countryCode = arrows.get(1);
WebElement timezone = arrows.get(2);
List arrows=driver.findElements(By.className(“选择箭头”);
WebElement jobTitle=arrows.get(0);
WebElement countryCode=arrows.get(1);
WebElement时区=arrows.get(2);

是,您可以使用
driver.findElements
(注意“s”)按索引选择它们。这将返回定位器找到的WebElement列表。然后,您可以将该列表作为任何列表进行迭代。例如:

List<WebElement> arrows = driver.findElements(By.className("Select-arrow"));
WebElement jobTitle = arrows.get(0);  
WebElement countryCode = arrows.get(1);
WebElement timezone = arrows.get(2);
List arrows=driver.findElements(By.className(“选择箭头”);
WebElement jobTitle=arrows.get(0);
WebElement countryCode=arrows.get(1);
WebElement时区=arrows.get(2);