C# 为什么Selenium不能读取下拉列表文本,即使这些文本存在于HTML中。(C、VS2019、硒)

C# 为什么Selenium不能读取下拉列表文本,即使这些文本存在于HTML中。(C、VS2019、硒),c#,html,selenium,combobox,dropdownbox,C#,Html,Selenium,Combobox,Dropdownbox,我正在尝试制作一个自动工具来控制网页。 我使用C、VisualStudio2019、Selenium和Windows10 然而,当我试图从下拉列表中选择一个值时,我无法读取它的文本。 以下是我的代码、HTML和结果 !!密码 !!打印结果 选择权 选择权 选择权 选择权 选择权 选择权 选择权 !!来自oSelection的错误消息。SelectByValue[21 MMI-H3]E/E OpenQA.Selenium.NoSuchElementException:“无法找到值为:[21 MMI

我正在尝试制作一个自动工具来控制网页。 我使用C、VisualStudio2019、Selenium和Windows10

然而,当我试图从下拉列表中选择一个值时,我无法读取它的文本。 以下是我的代码、HTML和结果

!!密码

!!打印结果

选择权

选择权

选择权

选择权

选择权

选择权

选择权

!!来自oSelection的错误消息。SelectByValue[21 MMI-H3]E/E

OpenQA.Selenium.NoSuchElementException:“无法找到值为:[21 MMI-H3]E/E的选项”

!!HTML! [21 I/EE-5 9]E/E [11 MIB-STD MO]E/E [11 MIB-STD AU]E/E [21 MIB-高MO]E/E [21 MIB-高金]E/E [21 MMI-H3]E/E [21 I/EE-5 9]E/E @迪利普梅格瓦尔 我尝试了Python,但正如您在上图中看到的,文本是。 我想这是根本原因,我不知道为什么。。。
通过Selenium等待功能、睡眠和手动暂停,我已经等得够久了。

代码中提供的-And/in[11 MIB-STD MO]E/E两侧都有空间。删除它,然后用byText重试。@dilimpeghwal哦,你说得对。但奇怪的是,我复制并粘贴了HTML中的关键字。无论如何,我会重试!但是,您知道为什么我不能将文本打印输出吗?同样的方法也适用于Python。我认为你必须等到元素被正确加载后再继续。
 _driverSerivce = ChromeDriverService.CreateDefaultService();
 _driverSerivce.HideCommandPromptWindow = true;
 _options = new ChromeOptions();
 ((ChromeOptions)_options).AddArgument("no-sandbox");
 _driver = new ChromeDriver((ChromeDriverService)_driverSerivce, (ChromeOptions)_options);

 wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(30));
 string URL = "Website";  // 
_driver.Navigate().GoToUrl(URL);

// I get the keyword below from HTML.
string Keyword = "erfassenForm:erfassenForm:workflowErfassungSelection:workflowErfassungSelection_input";
IWebElement SelectedElement = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.Id(Keyword)));

SelectElement oSelection = new SelectElement(SelectedElement);               
foreach (IWebElement element in oSelection.Options)
{
    System.Diagnostics.Debug.WriteLine(element.Text);        // Print Each Value's Text (But null is printed!!!)
    System.Diagnostics.Debug.WriteLine(element.TagName);     // Print Each Value's Tag (Correct tag name is printed.)
}

oSelection.SelectByValue("[21 MMI - H3 ] E / E");    // Select a value from Dropdown.