【问题标题】:In ruby, I can't get byebug to work with selenium在红宝石中,我无法让 byebug 与硒一起工作
【发布时间】:2018-12-28 17:40:06
【问题描述】:
require 'selenium-webdriver'
require 'byebug'

byebug

driver = Selenium::WebDriver.for:chrome

driver.navigate.to "http://google.com"

puts driver.find_element(:tag_name, 'input');
puts driver.find_element(:name, 'q');

driver.find_element(:name, 'q').send_keys("asdf");
#sleep 20

当我运行程序时,它直接转到driver = Selenium::WebDriver.for:chrome

我点击“n”,然后它加载 Chrome,然后转到 google 并将这些键发送到输入框并退出程序。

我想要发生的是,我点击“n”它会转到driver.navigate.to "http://google.com"。我再次按“n”,它转到puts driver.find_element(:tag_name, 'input');。我想从控制台运行诸如 driver.find_element 之类的命令。但我不能,因为 byebug 没有在 driver = Selenium::WebDriver.for:chrome 行之后跟踪程序。

我希望能够从控制台/irb/byebug 中使用 Selenium 签出 DOM

已添加

评论建议使用 pry

所以我尝试了require 'pry' 和binding.pry。和'next'而不是'n'(因为pry使用'next')。同样的问题。

我尝试了require 'pry-byebug' 和'binding.pry' 和'next' 而不是'n'(因为pry-byebug 使用'next')。同样的问题。

根据 rs 的建议,我尝试了 require 'pry' 和 require 'pry-byebug' 以及 binding.pry(停止)。并尝试step 而不是next。另外,同样的问题。我现在已经添加了这个的输出。

进一步补充

Apples-MBP:rubyselenium1 apple$ cat selenium12.rb 
# https://stackoverflow.com/questions/53962313/in-ruby-i-cant-get-byebug-to-work-with-selenium?noredirect=1#comment94763217_53962313

    require 'selenium-webdriver'
    require 'pry'
    require 'pry-byebug'

    binding.pry

    driver = Selenium::WebDriver.for:chrome

    driver.navigate.to "http://google.com"

    puts driver.find_element(:tag_name, 'input');
    puts driver.find_element(:name, 'q');

    driver.find_element(:name, 'q').send_keys("asdf");
    #sleep 20

Apples-MBP:rubyselenium1 apple$ ruby selenium12.rb 
WARN: Unresolved specs during Gem::Specification.reset:
      ffi (>= 1.0.11, ~> 1.0)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.

From: /Users/apple/rubyselenium1/selenium12.rb @ line 9 :

     4:     require 'pry'
     5:     require 'pry-byebug'
     6:  
     7:     binding.pry
     8:     
 =>  9:     driver = Selenium::WebDriver.for:chrome
    10:     
    11:     driver.navigate.to "http://google.com"
    12:  
    13:     puts driver.find_element(:tag_name, 'input');
    14:     puts driver.find_element(:name, 'q');

[1] pry(main)> step

From: /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver.rb @ line 86 Selenium::WebDriver.for:

    85: def self.for(*args)
 => 86:   WebDriver::Driver.for(*args)
    87: end

[1] pry(Selenium::WebDriver)> step

From: /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/driver.rb @ line 42 Selenium::WebDriver::Driver.for:

    41: def for(browser, opts = {})
 => 42:   case browser
    43:   when :chrome
    44:     Chrome::Driver.new(opts)
    45:   when :internet_explorer, :ie
    46:     IE::Driver.new(opts)
    47:   when :safari
    48:     Safari::Driver.new(opts)
    49:   when :phantomjs
    50:     PhantomJS::Driver.new(opts)
    51:   when :firefox, :ff
    52:     Firefox::Driver.new(opts)
    53:   when :edge
    54:     Edge::Driver.new(opts)
    55:   when :remote
    56:     Remote::Driver.new(opts)
    57:   else
    58:     raise ArgumentError, "unknown driver: #{browser.inspect}"
    59:   end
    60: end

[1] pry(Selenium::WebDriver::Driver)> step

From: /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/driver.rb @ line 44 Selenium::WebDriver::Driver.for:

    41: def for(browser, opts = {})
    42:   case browser
    43:   when :chrome
 => 44:     Chrome::Driver.new(opts)
    45:   when :internet_explorer, :ie
    46:     IE::Driver.new(opts)
    47:   when :safari
    48:     Safari::Driver.new(opts)
    49:   when :phantomjs
    50:     PhantomJS::Driver.new(opts)
    51:   when :firefox, :ff
    52:     Firefox::Driver.new(opts)
    53:   when :edge
    54:     Edge::Driver.new(opts)
    55:   when :remote
    56:     Remote::Driver.new(opts)
    57:   else
    58:     raise ArgumentError, "unknown driver: #{browser.inspect}"
    59:   end
    60: end

[1] pry(Selenium::WebDriver::Driver)> step

From: /usr/local/Cellar/ruby/2.5.0/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb @ line 40 Kernel#require:

    35:   #
    36:   # The normal <tt>require</tt> functionality of returning false if
    37:   # that file has already been loaded is preserved.
    38: 
    39:   def require path
 => 40:     RUBYGEMS_ACTIVATION_MONITOR.enter
    41: 
    42:     path = path.to_path if path.respond_to? :to_path
    43: 
    44:     if spec = Gem.find_unresolved_default_spec(path)
    45:       Gem.remove_unresolved_default_spec(spec)

[1] pry(main)> step

From: /usr/local/Cellar/ruby/2.5.0/lib/ruby/2.5.0/monitor.rb @ line 184 MonitorMixin#mon_enter:

    183: def mon_enter
 => 184:   if @mon_owner != Thread.current
    185:     @mon_mutex.lock
    186:     @mon_owner = Thread.current
    187:     @mon_count = 0
    188:   end
    189:   @mon_count += 1
    190: end

[1] pry(#<Monitor>)> step

From: /usr/local/Cellar/ruby/2.5.0/lib/ruby/2.5.0/monitor.rb @ line 185 MonitorMixin#mon_enter:

    183: def mon_enter
    184:   if @mon_owner != Thread.current
 => 185:     @mon_mutex.lock
    186:     @mon_owner = Thread.current
    187:     @mon_count = 0
    188:   end
    189:   @mon_count += 1
    190: end

[1] pry(#<Monitor>)> step
before_session hook failed: ThreadError: deadlock; recursive locking
/usr/local/Cellar/ruby/2.5.0/lib/ruby/2.5.0/monitor.rb:185:in `lock'
(see _pry_.hooks.errors to debug)
[1] pry(#<Monitor>)> 
Traceback (most recent call last):
    29: from selenium12.rb:9:in `<main>'
    28: from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver.rb:86:in `for'
    27: from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/driver.rb:44:in `for'
    26: from /usr/local/Cellar/ruby/2.5.0/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:40:in `require'
    25: from /usr/local/Cellar/ruby/2.5.0/lib/ruby/2.5.0/monitor.rb:186:in `mon_enter'
    24: from /usr/local/lib/ruby/gems/2.5.0/gems/byebug-10.0.2/lib/byebug/context.rb:98:in `at_line'
    23: from /usr/local/lib/ruby/gems/2.5.0/gems/pry-byebug-3.6.0/lib/byebug/processors/pry_processor.rb:63:in `at_line'
    22: from /usr/local/lib/ruby/gems/2.5.0/gems/pry-byebug-3.6.0/lib/byebug/processors/pry_processor.rb:111:in `resume_pry'
    21: from /usr/local/lib/ruby/gems/2.5.0/gems/pry-byebug-3.6.0/lib/byebug/processors/pry_processor.rb:27:in `run'
    20: from /usr/local/lib/ruby/gems/2.5.0/gems/pry-byebug-3.6.0/lib/byebug/processors/pry_processor.rb:27:in `catch'
    19: from /usr/local/lib/ruby/gems/2.5.0/gems/pry-byebug-3.6.0/lib/byebug/processors/pry_processor.rb:28:in `block in run'
    18: from /usr/local/lib/ruby/gems/2.5.0/gems/byebug-10.0.2/lib/byebug/helpers/eval.rb:94:in `allowing_other_threads'
    17: from /usr/local/lib/ruby/gems/2.5.0/gems/pry-byebug-3.6.0/lib/byebug/processors/pry_processor.rb:28:in `block (2 levels) in run'
    16: from /usr/local/lib/ruby/gems/2.5.0/gems/pry-byebug-3.6.0/lib/byebug/processors/pry_processor.rb:113:in `block in resume_pry'
    15: from /usr/local/lib/ruby/gems/2.5.0/gems/pry-0.11.3/lib/pry/pry_instance.rb:348:in `repl'
    14: from /usr/local/lib/ruby/gems/2.5.0/gems/pry-0.11.3/lib/pry/repl.rb:38:in `start'
    13: from /usr/local/lib/ruby/gems/2.5.0/gems/pry-0.11.3/lib/pry/input_lock.rb:79:in `with_ownership'
    12: from /usr/local/lib/ruby/gems/2.5.0/gems/pry-0.11.3/lib/pry/input_lock.rb:61:in `__with_ownership'
    11: from /usr/local/lib/ruby/gems/2.5.0/gems/pry-0.11.3/lib/pry/repl.rb:38:in `block in start'
    10: from /usr/local/lib/ruby/gems/2.5.0/gems/pry-0.11.3/lib/pry/repl.rb:67:in `repl'
     9: from /usr/local/lib/ruby/gems/2.5.0/gems/pry-0.11.3/lib/pry/repl.rb:67:in `loop'
     8: from /usr/local/lib/ruby/gems/2.5.0/gems/pry-0.11.3/lib/pry/repl.rb:68:in `block in repl'
     7: from /usr/local/lib/ruby/gems/2.5.0/gems/pry-0.11.3/lib/pry/repl.rb:108:in `read'
     6: from /usr/local/lib/ruby/gems/2.5.0/gems/pry-0.11.3/lib/pry/indent.rb:394:in `correct_indentation'
     5: from /usr/local/lib/ruby/gems/2.5.0/gems/pry-0.11.3/lib/pry/terminal.rb:8:in `screen_size'
     4: from /usr/local/lib/ruby/gems/2.5.0/gems/pry-0.11.3/lib/pry/terminal.rb:33:in `actual_screen_size'
     3: from /usr/local/lib/ruby/gems/2.5.0/gems/pry-0.11.3/lib/pry/terminal.rb:47:in `screen_size_according_to_io_console'
     2: from /usr/local/Cellar/ruby/2.5.0/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:40:in `require'
     1: from /usr/local/Cellar/ruby/2.5.0/lib/ruby/2.5.0/monitor.rb:185:in `mon_enter'
/usr/local/Cellar/ruby/2.5.0/lib/ruby/2.5.0/monitor.rb:185:in `lock': deadlock; recursive locking (ThreadError)
Apples-MBP:rubyselenium1 apple$ 
Apples-MBP:rubyselenium1 apple$ 

【问题讨论】:

  • 听起来它正在工作....但是,您总是可以尝试 pry 代替
  • @maxpleaner 刚刚尝试了 pry 甚至 pry-byebug,并在我的问题中添加了一条注释。同样的问题
  • 如果你使用step而不是next会发生什么?
  • @TomLord pry 说“before_session 挂钩失败:ThreadError:死锁;递归锁定 /usr/local/Cellar/ruby/2.5.0/lib/ruby/2.5.0/monitor.rb:185:在 `lock' 中(请参阅 pry.hooks.errors 进行调试)"
  • @barlop 如果您不想进入代码而只需要从一个语句转到另一个语句,请使用 next,我在上面的代码中看到它正确停止,下一个应该带你to : driver.navigate.to "google.com" 然后到下面的行。

标签: ruby selenium irb byebug


【解决方案1】:

此 gem“添加了逐步调试和堆栈导航功能以使用 byebug 进行撬动。” {quoted from pry-byebug},所以你需要 pry 和 byebug 才能使用 pry byebug。因此,请确保安装了两个 gem:您可以将两个 gem 添加到您的 GemFile 并执行 bundle install 或直接执行此操作,之后您应该要求两者然后添加 binding.pry 执行将在之后的第一个语句处停止。

“别名'n', 's', 'c' and 'f'被默认删除,因为它们通常与临时变量名冲突。但是如果你仍然需要它们,重新启用它们非常容易,只需将以下快捷方式添加到你的 ~/.pryrc 文件:“

if defined?(PryByebug)
  Pry.commands.alias_command 'c', 'continue'
  Pry.commands.alias_command 's', 'step'
  Pry.commands.alias_command 'n', 'next'
  Pry.commands.alias_command 'f', 'finish'
end

我在上面提供的链接中提供了更多详细信息,希望对您有所帮助。

由 barlop 添加

rs cmets 提问者尝试require pry 和require pry-byebug 并用binding.pry 停止

【讨论】:

  • 这完全无关紧要,我很清楚有一种方法可以将 'n' 映射为 'next' pry-byebug。这根本不是我要问的
  • @barlop 所以有几件事导致了这个答案:第一,你正在使用 byebug 来停止执行,这是不正确的,所以我建议 binding.pry,pry 在上面的代码中不需要,所以我相信这会导致你的执行不会停止,所以我建议要求它,最后你对点击“n”的评论让我相信你可能没有将这些别名添加到 ~/.pryrc 文件中:”,所以在我的评估答案并非无关紧要,除非我错过了您的实际问题,但是,如果您觉得答案没有帮助,我会留给您最好的判断
  • 谢谢。但是请参阅我已经添加到问题中的注释,它表明我也尝试了require 'pry'/binding.pry。 (即我在没有 byebug 的情况下尝试过)
  • 我已经在我的项目中成功地使用带和不带 byebug 的 pry 有一段时间了,不知道为什么它会导致问题,我可以建议的唯一额外的事情是你可以检查你的 Gemset 和 gemlist确保您使用的是正确的 gemset ( rvm gemset list) 并且 gemset 已在其中窥探 (gem list)。您可能已经这样做了,但这只是一个建议。
  • 我没有使用 rvm,但如果我使用 gem list,它会显示 pry (0.11.3, 0.10.4)
【解决方案2】:

以下是部分答案。它没有解决 pry / byebug / pry-byebug 问题,但它确实解决了这部分问题,

问题之一是

“我希望能够从控制台使用 Selenium 签出 DOM /irb/byebug"

使用 Selenium 的代码在直接输入 irb 时运行正常

$ irb
irb(main):001:0> require 'selenium-webdriver'
=> true

irb(main):011:0> driver = Selenium::WebDriver.for:firefox
=> #<Selenium::WebDriver::Firefox::Marionette::Driver:0x..fcead308af2433ac2 browser=:firefox>

irb(main):013:0> driver.navigate().to("http://www.google.com")
=> nil

irb(main):036:0> inps=driver.find_elements(:tag_name, "input");
=> [#<Selenium::WebDriver::Element:0x..f8ed35db92a849eea id="dac....

irb(main):041:0> inps[2].attribute('name');
=> "q"

irb(main):042:0> inps[2].attribute('value');
=> ""  <-- shows whatever is typed in that google search box eg prior to checking the value we could've typed something e.g. 'asdf' into it manually or programmatically eg driver.find_element(:tag_name, "input").send_keys("asdf");  and it will show "asdf".

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多