【问题标题】:Why Selenium can't read texts of dropdown even though those exist in HTML. (C#, VS2019, Selenium)为什么 Selenium 无法读取下拉列表的文本,即使它们存在于 HTML 中。 (C#,VS2019,硒)
【发布时间】:2020-12-31 04:18:11
【问题描述】:

我正在尝试制作一个自动工具来控制网页。 我使用 C#、Visual Studio 2019、Selenium 和 Windows 10。

但是,当我尝试从下拉列表中选择一个值时,我无法阅读其文本。 以下是我的代码、HTML 和结果。

!代码!

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

!打印结果!

选项

选项

选项

选项

选项

选项

选项

!来自 oSelection.SelectByValue("[21 MMI - H3 ] E / E") 的错误消息!

OpenQA.Selenium.NoSuchElementException: '找不到具有值的选项:[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>

正如您在结果部分中看到的那样,“element.Text”并没有打印任何内容并被选中。 但是,在 HTML 中,有一些值,例如 '[11 MIB - STD MO] E/E'。

你能告诉我原因吗? 现在我想知道我是否使用正确的关键字来选择下拉列表。 (我使用'erfassenForm:erfassenForm:workflowErfassungSelection:workflowErfassungSelection_input')

感谢您的关注。

【问题讨论】:

  • 代码中提供的[11 MIB - STD MO] E/E-/两边都有空格。删除它并使用 byText 重新调用。
  • @DilipMeghwal 哦,你是对的。但奇怪的是我从 HTML 复制并粘贴了关键字。无论如何我会重试的!但是你知道为什么我不能打印输出的文本吗?
  • 使用 Python 的方法相同。我认为你必须等到元素正确加载然后继续。

标签: c# html selenium combobox dropdownbox


【解决方案1】:

@DilipMeghwal 我尝试了 Python,但正如您在上图中看到的那样,文本是 ''。 我认为这是根本原因,我不知道为什么...... 我通过 Selenium 等待功能、睡眠和手动暂停等了足够的时间。

【讨论】:

    猜你喜欢
    • 2015-12-22
    • 2020-01-02
    • 1970-01-01
    • 2018-07-27
    • 1970-01-01
    • 2013-04-22
    • 2016-11-13
    • 1970-01-01
    • 2012-12-06
    相关资源
    最近更新 更多