【问题标题】:selenium and python click element in a dropdown下拉列表中的 selenium 和 python click 元素
【发布时间】:2019-03-04 14:30:15
【问题描述】:

仍然是 selenium 的新手,xpath 大部分时间都很好用,但在这种情况下不能使用下拉框从下拉框中选择元素:

盒子的xpath是:

//*[@id="select2-report_object-container"]

我让浏览器点击它,现在它显示一个选项下拉列表, 现在我想从列表中选择元素“标题”APP 7.08。

xpath 不起作用,因为它只显示容器

//*[@id="select2-report_object-container"]

要选择的元素如下:

<span class="select2-selection__rendered" id="select2-report_object-container" title="APP 7.08">APP 7.08</span>

标签名称不起作用,链接名称也可以,也尝试使用 id 然后是标题值

消息:

没有这样的元素:无法定位元素

我也试过了

selenium.select('//*[@id="select2-report_object-container"]' , title="APP 7.08";

运气不好。

我觉得我需要向初始 xpath 或 ID 添加第二个参数,但不确定使用什么来从 span 调用 APP 7.08 的值。

 <span class="select2-selection__rendered" id="select2-report_object-container" title="APP 7.08">APP 7.08</span>

谁能帮忙?

【问题讨论】:

  • 可以分享一下html代码吗?对于选择标签,您可以使用 getIndex()、getValue、getText 方法来获取您的值
  • 这是可用的:在位置类的 html 中,值 17 是我需要的。

标签: python selenium select xpath drop-down-menu


【解决方案1】:

我假设您知道要选择哪个选项,因此您可以使用可见文本或值来完成它。

select = Select(driver.find_element_by_id('select2-report_object-container'))

# select by visible text
select.select_by_visible_text('title')

# select by value 
select.select_by_value('title')

【讨论】:

  • 以上没有用。我也认为你的意思是输入 selenium.select_by_visible_text('APP 7.08') 我试过 driver.find_element_by_id('select2-report_object-container').click() selenium.select_by_visible_text('APP 7.08') 但没有运气
  • 你有什么错误吗?你试过选择吗?为什么是 selenium.select_by_visible_text ?
【解决方案2】:

你可以试试下面的代码:-

 select= select(driver.find_element_by_name('location')) 
    select.select_by_value(17) or
 select.select_by_visible_text('APP 7.08') 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多