代码之家  ›  专栏  ›  技术社区  ›  Benjamin Bouchet

如何在geckodriver中禁用纯文本.wrap\u long\u行选项?

  •  0
  • Benjamin Bouchet  · 技术社区  · 6 年前

    在rspec中使用seleniumwebdriver和capybara,在features规范中,我尝试获取纯文本请求的HTTP响应,即/robots.txt

    但是我没有得到纯文本响应,而是得到了用HTML包装的文本响应:

       expected: "User-agent: *\nDisallow:\n\nSitemap: https://prj.org/sitemap.xml\n"
            got: "<html><head><link rel=\"alternate stylesheet\" type=\"text/css\" href=\"resource://content-accessible/plaintext.css\" title=\"Wrap Long Lines\"></head><body><pre>User-agent: *\nDisallow:\n\nSitemap: https://prj.org/sitemap.xml\n</pre></body></html>"
    

    使用curl获取/robots.txt时,我得到了预期的纯文本响应。所以我浏览了Firefox选项,发现我需要禁用它 选项。

    我也不能成功地把选择权交给geckodriver。

    Options 对象,如下所示:

    Capybara.register_driver :firefox_headless do |app|
      options = ::Selenium::WebDriver::Firefox::Options.new
      options.headless!
      options.add_preference 'plain_text.wrap_long_lines', false
    
      Capybara::Selenium::Driver.new app, browser: :firefox, options: options
    end
    

    然后我试着把它传给一个 Profile 对象。

    Capybara.register_driver :firefox_headless do |app|
      options = ::Selenium::WebDriver::Firefox::Options.new
      options.headless!
    
      profile = Selenium::WebDriver::Firefox::Profile.new
      profile['plain_text.wrap_long_lines'] = false
    
      Capybara::Selenium::Driver.new app, browser: :firefox, options: options, profile: profile
    end
    

    在这两种情况下,结果是相同的。你知道为什么吗?谢谢!

    • selenium webdriver 3.14.1版
    • 壁虎河0.22.0
    1 回复  |  直到 6 年前
        1
  •  0
  •   Thomas Walpole    6 年前

    # If using minitest
    visit('/robots.txt')
    find('body > pre').assert_text("User-agent: *\nDisallow:\n\nSitemap: https://prj.org/sitemap.xml\n", exact: true)
    
    # If using RSpec
    visit('/robots.txt')
    expect(find('body > pr')).to have_text("User-agent: *\nDisallow:\n\nSitemap: https://prj.org/sitemap.xml\n", exact: true)