【问题标题】:How to Select a dropdown list in a javascrip site using selenium in python3?如何在 python3 中使用 selenium 在 javascript 站点中选择下拉列表?
【发布时间】:2021-07-17 13:56:31
【问题描述】:

我被困在以下 html 代码中,该代码取自一个使用 Javascript 的站点。我想要的是使用 Select 模块在 Selenium 中选择项目“Short_Budget_Report”。 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;">

我尝试过的:

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

这会产生以下错误:

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

【问题讨论】:

  • 有一些与您的 xpath 相关的错误。可能是您选择了错误元素的 xpath。请分享网站链接,以便我可以帮助您
  • 您似乎在输入标签而不是下拉菜单

标签: javascript python python-3.x selenium


【解决方案1】:

您可以简单地使用send_keys() 函数在下拉列表中选择特定选项。

  1. 首先通过find_element()函数中的任意一个定位下拉列表的元素
  2. 在该元素上使用 send_keys() 函数立即从列表中选择任何值

简而言之,按照以下方式进行操作应该可以完成您需要的操作:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-06
    • 2021-09-19
    • 1970-01-01
    • 2021-01-08
    • 2014-07-31
    • 2014-07-23
    • 2021-06-11
    • 2022-10-13
    相关资源
    最近更新 更多