【问题标题】:Selecting all options from select then iterate through values to find selected value - Selenium Webdriver从 select 中选择所有选项,然后遍历值以找到选定的值 - Selenium Webdriver
【发布时间】:2016-07-29 21:26:57
【问题描述】:

我在提取下拉列表中的所有选项然后遍历值以获取选定值时遇到问题。 Ruby代码如下:

select = @@driver.find_element(:id, 'dropdown_7')
            all_options = select.find_elements(:tag_name, 'option')
            all_options.each do |i|
              puts 'Value is: ' + i.attribute('Andorra')
              i.click

HTML 代码:

<select id="dropdown_7" name="dropdown_7" class="  piereg_validate[required]"><option value="Afghanistan">Afghanistan</option></select>

错误消息:`+':没有将 nil 隐式转换为字符串 (TypeError)

除了 + = nil 并且没有字符串转换之外,不确定这意味着什么?

【问题讨论】:

    标签: ruby-on-rails ruby selenium selenium-webdriver


    【解决方案1】:

    由于i.attribute('Andorra') 返回了nil ruby​​ 未能转换为字符串,因此引发错误。这里有几个例子可以让你得到你想要的:

    # print the name attribute
    puts 'Name is: %s' % i.attribute('name')
    
    # print the value attribute
    puts 'Value is: %s' % i.attribute('value')
    
    # print the text content
    puts 'Text is: %s' % i.text
    

    【讨论】:

    • 好的,谢谢,它现在正在遍历值,但是当它找到 Andorra 的选定属性时,它没有选择它只是继续迭代下拉列表中的值,当它需要选择 Andorra一旦它在选项列表中找到它。顺便说一句,对于 ruby​​ 来说还是新手, % 是如何工作的?我以为这仅用于数学?
    • 百分比用于格式化字符串。至于您的问题,没有足够的信息来提供更好的解决方案。看看这个例子,它可能会对你有所帮助:stackoverflow.com/questions/4672658/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 2017-08-19
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    相关资源
    最近更新 更多