使用 xpath 从选择列表中获取值

发布于 2024-12-15 03:08:04 字数 1523 浏览 3 评论 0原文

不起作用

puts   $browser.select_list(:xpath,"//*[@id='numType']").exists?

number = $browser.select_list(:xpath,"//*[@id='numType']").options.map(&:text)

 number_list = Array.new
 number.each do |number_text|
      number_list << "#{number_text}"
 end

我正在尝试使用 xpath 获取选择列表值,因为 by id 在存在函数中给了我 false,但HTML 代码

<div id="forwardRs">
<div class="forwardR">
<div id="forwardT" class="rc1">
  <select id="forward_types" name="Foward Types" tabindex="5" onchange="changeForwardTypes(this);">
    <option value="unconditional">unconditional</option>
    <option value="busy">busy</option>
    <option value="reply">reply</option>
    <option value="reachable">reachable</option>
    <option value="other">other</option>
  </select>
</div>
<div id="numtype" class="rc1" style="">
  <select id="numType" onchange="changeNumType(this);" name="numbers" tabindex="5">
    <option value="ex">Ex</option>
    <option value="in">In</option>
  </select>
</div>
<div id="rule" class="rc1" style="">
  <input id="tel" type="text" value="" size="20" name="tel" tabindex="2" onchange="changeNumberRule(this);">
</div>
<div id="removeForwardRule" class="rc3">
  <a onclick="removeForwardRule(this);">
   <img src="../../images/delete.png">
  </a>

我该怎么做?

坦克

I'm trying to get the select list values using xpath because by id is giving me false in the exists function, but is not working

puts   $browser.select_list(:xpath,"//*[@id='numType']").exists?

number = $browser.select_list(:xpath,"//*[@id='numType']").options.map(&:text)

 number_list = Array.new
 number.each do |number_text|
      number_list << "#{number_text}"
 end

HTML code:

<div id="forwardRs">
<div class="forwardR">
<div id="forwardT" class="rc1">
  <select id="forward_types" name="Foward Types" tabindex="5" onchange="changeForwardTypes(this);">
    <option value="unconditional">unconditional</option>
    <option value="busy">busy</option>
    <option value="reply">reply</option>
    <option value="reachable">reachable</option>
    <option value="other">other</option>
  </select>
</div>
<div id="numtype" class="rc1" style="">
  <select id="numType" onchange="changeNumType(this);" name="numbers" tabindex="5">
    <option value="ex">Ex</option>
    <option value="in">In</option>
  </select>
</div>
<div id="rule" class="rc1" style="">
  <input id="tel" type="text" value="" size="20" name="tel" tabindex="2" onchange="changeNumberRule(this);">
</div>
<div id="removeForwardRule" class="rc3">
  <a onclick="removeForwardRule(this);">
   <img src="../../images/delete.png">
  </a>

How can i do this?

Tanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抱着落日 2024-12-22 03:08:04

忽略我最初在这里写的内容,因为我完全没有成功地让 .options 方法返回选择列表中的任何选项,而 .selected_options 方法返回所选择的选项就好了。

我已经能够通过执行以下操作成功地从您的选择列表中获取选项:

select_list_options = $ff.elements_by_xpath("//select[@id='numType']/option")

这为您提供了选择列表中的选项数组。

为了从这些元素中获取文本,我没有给出

select_list_options_text = Array.new
select_list_options.each do |option|
  select_list_options_text << option.text
end

一个非常漂亮的答案,但是嘿。

我真的不知道为什么 .options 对我不起作用,但您可能遇到同样的问题。

Disregard what I originally wrote here, as I have been completely unsuccessful in getting the .options method to return any of the options in a select list, whereas the .selected_options method returns the selected options just fine.

I've been able to successfully get the options from your select list by doing the following:

select_list_options = $ff.elements_by_xpath("//select[@id='numType']/option")

which gives you an array of the options in the select list.

To get the text out of those elements I did

select_list_options_text = Array.new
select_list_options.each do |option|
  select_list_options_text << option.text
end

Not a very pretty answer but hey.

I have literally no idea why .options does not work for me, but you may be suffering the same problem.

绝不放开 2024-12-22 03:08:04

我已经使用问题中的 html 创建了 html 文件,出于某种原因,我的 Firefox 8 认为 option 标签不在 select 标签内。请参阅随附的屏幕截图。在此处输入图像描述

I have created html file with the html from the question, and my Firefox 8 for some reason thinks that option tags are not inside select tag. See attached screen shot.enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文