如何通过selenium驱动程序从“<div class='uihelperhiddenaccessible'>”中提取选项?

2024-06-28 20:24:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用selenium和python对这个站点进行爬网:https://ntrl.ntis.gov/NTRL

但是当我想更改下拉列表的年份时,它就不能工作了。在

下面是它的HTML:

<div id="advSearchForm:FromYear" class="ui-selectonemenu ui-widget ui-state-default ui-corner-all" style="min-width: 63px;">
    <div class="ui-helper-hidden-accessible">
        <input id="advSearchForm:FromYear_focus" name="advSearchForm:FromYear_focus" type="text" autocomplete="off" role="combobox" aria-haspopup="true" aria-expanded="false" readonly="readonly" aria-autocomplete="list" aria-owns="advSearchForm:FromYear_items" aria-activedescendant="advSearchForm:FromYear_0" aria-describedby="advSearchForm:FromYear_0" aria-disabled="false">
    </div>
    <div class="ui-helper-hidden-accessible">
        <select id="advSearchForm:FromYear_input" name="advSearchForm:FromYear_input" tabindex="-1">
            <option value="*" selected="selected">&lt;1900</option>
            <option value="1900">1900</option>
            <option value="1901">1901</option>
            <option value="1902">1902</option>
            <option value="1903">1903</option>
        </select>
    </div>
    <label id="advSearchForm:FromYear_label" class="ui-selectonemenu-label ui-inputfield ui-corner-all">&lt;1900</label>
    <div class="ui-selectonemenu-trigger ui-state-default ui-corner-right">
        <span class="ui-icon ui-icon-triangle-1-s ui-c"/>
    </div>
</div>

以下是我的代码:

^{pr2}$

但也有例外:

Element is not currently visible and may not be manipulated

我尝试使用js脚本:

driver.execute_script("document.getElementById('advSearchForm:FromYear_input').options[2].selected = 'true'")

但是它也不起作用,我测试了select.select_by_value(xxx)可以在其他下拉列表中使用,所以这可能是{}的问题,那么我该如何处理呢?在


Tags: dividuiinputvalueselectlabelclass
1条回答
网友
1楼 · 发布于 2024-06-28 20:24:29

我建议使用click事件单击元素(id为“advS”的Select元素earchForm:FromYear_输入“)首先,然后是ExplicitWait event以等待元素可见,然后您应该能够使用select_by_value方法更改年份。在

另外,我将避免使用XPath而使用CSS selector,更好的做法是创建一个Page Object Model,以减少在将来更新页面时保持工具正常工作所需的工作量。在

很抱歉,我不能提供更多帮助,我对python不太熟悉。在

你也可以参考this question.

编辑

看起来好像是在使用option中的option项作为主列表,而实际的选择发生在页面下方的另一个元素中。这个元素是用Javascript动态构建的,所以我在评论中的建议行不通。在

我用C语言编写了一个工作应用程序,让您了解您需要做什么:

private static void Main(string[] args)
{
    // ':' has a special meaning in CSS selectors so we need to escape it using \\
    const string dropdownButtonSelector = "div#advSearchForm\\:datePublPanel div.ui-selectonemenu-trigger";
    // {0} is a placeholder which is used to insert text during runtime
    const string dynamicallyBuiltListItemSelectorTemplate = "ul#advSearchForm\\:FromYear_items li[data-label=\"{0}\"]";
    // Rather than being a constant this value will be determined at runtime
    const string valueToSelect = "1902";

    // Setup driver and wait
    ChromeDriver driver = new ChromeDriver();
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));

    // Load page
    driver.Navigate().GoToUrl("https://ntrl.ntis.gov/NTRL/");
    // Wait until the first (index 0) dropdown list button inside the publication date dive is deemed "clickable"
    wait.Until(ExpectedConditions.ElementToBeClickable(driver.FindElementsByCssSelector(dropdownButtonSelector)[0]));

    Console.WriteLine("Element is visible");

    // Open the dropdown list
    driver.FindElementsByCssSelector(dropdownButtonSelector)[0].Click();

    Console.WriteLine("Dropdown should be open");

    // Select the element from the dynamic Javascript built list
    string desiredValueListItemSelector = string.Format(dynamicallyBuiltListItemSelectorTemplate, valueToSelect);
    driver.FindElementByCssSelector(desiredValueListItemSelector).Click();

    Console.WriteLine($"Selected value {valueToSelect} using selector: {desiredValueListItemSelector}");
    Console.ReadLine();

    driver.Close();
}

=================================================================================

编辑2

包括python答案,我以前从来没有写过python,但这似乎是可行的。我强烈建议查看我在上面发布的关于使用PageObject模型和显式等待的链接,以及避免使用XPATH选择器。在

^{pr2}$

相关问题 更多 >