简体   繁体   English

如何在python3中使用selenium在javascrip站点中Select下拉列表?

[英]How to Select a dropdown list in a javascrip site using selenium in python3?

I am stuck in the following html code taken from a site that uses Javascript.我被困在以下 html 代码中,该代码取自使用 Javascript 的站点。 What I want is to select the item "Short_Budget_Report" in Selenium using Select moduele.我想要的是使用 Select 模块 Selenium 中的 select 项目“Short_Budget_Report”。 The html code is as follows: html代码如下:

<input id="WD51" ct="CB" lsdata="{1:'20ex',8:'WD52',9:'2347',11:'Short_Budget_Report',14:'Load\x20View',18:'View',44:false,48:'WD51\x2dtlbl'}" lsevents="{Select:[{ResponseData:'delta',ClientAction:'submit'},{}]}" type="text" autocomplete="off" tabindex="0" ti="0" title="Load View" class="lsField__input urEdf2TxtEnbl lsEdfLeftBrdRadius lsEdf3TxtHlpBtn urEdfVAlign urBorderBox lsControl--explicitwidth" readonly="" value="Short_Budget_Report" style="vertical-align:top;width:20ex;">

What I have tried:我试过的:

dropdown_id = driver.find_element_by_xpath('//*[@id="WD51"]')
    dropdown = Select(dropdown_id)
    dropdown.select_by_value('Short_Budget_Report')

This gives the following error:这给出了以下错误:

raise UnexpectedTagNameException(
selenium.common.exceptions.UnexpectedTagNameException: Message: Select only works on <select> elements, not on <input>

You can simply use send_keys() function to select particular option in dropdown list.您可以简单地使用send_keys() function 到 select 下拉列表中的特定选项。

  1. First locate the element of dropdown list by any of find_element() function首先通过find_element() function 中的任何一个找到下拉列表的元素
  2. use send_keys() function on that element to select any value from list right away立即在该元素上使用send_keys() function 到 select 列表中的任何值

In short, doing it in following way should get done what you need:简而言之,按照以下方式进行操作应该可以完成您需要的操作:

driver.find_element_by_id('WD51').send_keys('Short_Budget_Report')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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