如何从下拉菜单中选择跨度下定义的元素

问题描述 投票:0回答:2
c# selenium-webdriver drop-down-menu html-select
2个回答
1
投票

这实际上不是一个 SELECT 元素,它是一个看起来像普通下拉列表的元素集合。您需要单击顶级元素以打开下拉列表,然后在所需元素可见后选择它。

单击“教授”的示例代码“称呼”标签下的选项:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[.//label[text()='Salutation']//div[@class='ant-select-selection']"))).Click();
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[.//label[text()='Salutation']//span[@class='ant-select-selection-item'][@title='Prof.']"))).Click();

我假设你会不止一次地使用它......你需要设置称呼,名字,姓氏等。我会把它放在一个方法中以使其更易于使用。

/// <summary>
/// Selects an option from the dropdown based on the label and desired option.
/// </summary>
/// <param name="label">The dropdown label.</param>
/// <param name="option">The option to be chosen from the dropdown.</param>
public void SelectOption(string label, string option)
{
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath($"//div[.//label[text()='{label}']//div[@class='ant-select-selection']"))).Click();
    wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath($"//div[.//label[text()='{label}']//span[@class='ant-select-selection-item'][@title='{option}']"))).Click();
}

然后像这样称呼它

SelectOption("Salutation", "Prof.");

0
投票

在杰夫的帮助下过了一会儿。我能够从下拉列表中选择元素。这是一个代码 ///扩展下拉列表

WebDriverWait wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(50));

wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id=\"root\"]/main/section/form/div/div[1]/div[1]/div[1]/div/div"))).Click();

///从下拉列表中选择元素

wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("/html/body/div[3]/div/div/div/div[2]/div[1]/div/div/div[1]/div"))).Click();
© www.soinside.com 2019 - 2024. All rights reserved.