简体   繁体   中英

Why Selenium can't read texts of dropdown even though those exist in HTML. (C#, VS2019, Selenium)

I'm trying to make an automatic tool to control a web-page. I use C#, Visual studio 2019, Selenium and Windows 10.

However when I attempted to select a value from a dropdown list, I can't read its texts. The followings are my code, HTML and the result.

!Code!

 _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.

!Print Result!

option

option

option

option

option

option

option

!Error message from oSelection.SelectByValue("[21 MMI - H3 ] E / E")!

OpenQA.Selenium.NoSuchElementException: 'Cannot locate option with value: [21 MMI - H3 ] E / E'

!HTML!

 <div class="worflowSelectionDropdown"> <div id="erfassenForm:erfassenForm:workflowErfassungSelection:workflowErfassungSelection_wrapper" class="select-box "> <div id="erfassenForm:erfassenForm:workflowErfassungSelection:workflowErfassungSelection" class="ui-selectonemenu ui-widget ui-state-default ui-corner-all select closed "> <div class="ui-helper-hidden-accessible"> <input id="erfassenForm:erfassenForm:workflowErfassungSelection:workflowErfassungSelection_focus" name="erfassenForm:erfassenForm:workflowErfassungSelection:workflowErfassungSelection_focus" type="text" autocomplete="off" role="combobox" aria-haspopup="true" aria-expanded="false" aria-autocomplete="list" aria-owns="erfassenForm:erfassenForm:workflowErfassungSelection:workflowErfassungSelection_items" aria-activedescendant="erfassenForm:erfassenForm:workflowErfassungSelection:workflowErfassungSelection_1" aria-describedby="erfassenForm:erfassenForm:workflowErfassungSelection:workflowErfassungSelection_1" aria-disabled="false"> </div> <div class="ui-helper-hidden-accessible"> <select id="erfassenForm:erfassenForm:workflowErfassungSelection:workflowErfassungSelection_input" name="erfassenForm:erfassenForm:workflowErfassungSelection:workflowErfassungSelection_input" tabindex="-1" onchange="updateComboboxTooltip(PF(&#39;&#39;), &#39;workflowErfassungSelectionPanelTooltip&#39;); ;"> <option value="" selected="selected"></option> <option value="0" selected="selected">[21 I/EE-5 9] E/E</option> <option value="1">[11 MIB-STD MO] E/E</option> <option value="2">[11 MIB-STD AU] E/E</option> <option value="3">[21 MIB-HIGH MO] E/E</option> <option value="4">[21 MIB-HIGH AU] E/E</option> <option value="5">[21 MMI-H3 ] E/E</option> </select> </div> <label id="erfassenForm:erfassenForm:workflowErfassungSelection:workflowErfassungSelection_label" class="ui-selectonemenu-label ui-inputfield ui-corner-all">[21 I/EE-5 9] E/E</label> <div class="ui-selectonemenu-trigger ui-state-default ui-corner-right"> <span class="ui-icon ui-icon-triangle-1-s ui-c"></span> </div> </div> </div> <span id="erfassenForm:erfassenForm:workflowErfassungSelection:workflowErfassungSelectionPanelTooltip" class="tooltip right iconTooltip"></span> </div>

As you can see in the result section, nothing is printed with 'element.Text' and selected. However, in HTML, there are some values such as '[11 MIB - STD MO] E/E'.

Could you let me know the reason? Now I'm wondering if I used correct keyword to select the dropdown list. (I used 'erfassenForm:erfassenForm:workflowErfassungSelection:workflowErfassungSelection_input')

Thank you for your interest.

在此处输入图片说明

@DilipMeghwal I tried Python, but as you can see in the picture above, the texts were ''. I think this is the root cause which I don't know why... And I waited enough by Selenium wait function, sleep and manual pause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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