独角兽或硒问题?

时间:2012-08-09 17:12:07

标签: ruby-on-rails ruby unix selenium unicorn

我的服务器上运行Unicorn HTTP服务器的一个实例。 它会在此目录中加载一些文件:

~/apps/myapp/current/

此路径是此路径中最后一个版本的符号链接:

~/apps/myapp/releases/234234532/

这是由Capistrano在部署时配置的。 为了确保Unicorn重新启动,我也手动完成,即使我的deploy.rb包含一个在最后调用的deploy:restart任务,因为我读到Capistrano对Unicorn会有一些问题。

我的应用即将通过Selenium WebDriver启动Selenium实例。 然而,这就是我的问题,Selenium似乎指向一个旧版本。

可以肯定的是,我选择删除我的进程的关键文件,以便查看我的应用程序是否仍能正常启动,这是我预期的非常好的版本。 希望我的应用程序没有启动,并且出现了一些逻辑错误。

所以现在我确定我指出好的释放,我不理解Selenium的行为。我在解释:

日志中出现的错误是:

invalid page structure: Unable to locate element: {"method":"id","selector":"sectionSearch"}
Command duration or timeout: 1.02 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.21.0', revision: '16552', time: '2012-04-11 19:08:38'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '3.2.13-grsec-xxxx-grs-ipv6-64', java.version: '1.7.0_147-icedtea'
Driver info: driver.version: EventFiringWebDriver (org.openqa.selenium.NoSuchElementException)

总而言之,Selenium抱怨id元素:sectionSearch在相关的HTML页面中缺失。

所以,正如本文所述:http://seleniumhq.org/docs/04_webdriver_advanced.html,在通知特定的html元素不存在之前,我增加了超时:

wait {driver.find_element(:id, 'sectionSearch')}  

有关信息,请等待自定义方法:

def wait(timeout = 50, &block)
    Selenium::WebDriver::Wait.new(timeout: timeout).until(&block)
end

在本地计算机(开发环境)中,这可能是因为我使用了另一个SeleniumDriver:FirefoxDriver

def driver
    @driver ||= begin
      if Rails.env.production?
        driver = Selenium::WebDriver.for :remote, url: 'http://localhost:4444/wd/hub'
      else
        driver = Selenium::WebDriver.for :firefox
      end
      driver.manage.timeouts.implicit_wait = 20
      driver
    end
  end

但是在杀死并重新启动独角兽之后,我的服务器仍然存在同样的问题:

invalid page structure: Unable to locate element: {"method":"id","selector":"sectionSearch"}
    Command duration or timeout: 1.02 seconds

所以,我的最后一个动作是手动更改服务器的应用程序当前文件夹中的行:

wait {driver.find_element(:id, 'sectionSearch')} 

通过

wait {driver.find_element(:id, 'sectionSearchhhhhhhh')}

现在我的期望是看到Selenium抱怨这个缺失的元素,所以错误是合乎逻辑的。为什么这样做?因为我想确保Selenium获取UPDTATED源文件。

似乎事实并非如此......出现了与前一个相同的错误。

任何有想法的人?

1 个答案:

答案 0 :(得分:1)

这是一个硒错误,你得到它是因为你的webdriver在到达元素之前超时,或者因为你指出了错误的选择器(即 - >你得到错误元素未找到)。如果我是你,我将使用xpath作为选择器,因为它似乎是最可靠的定位器。我们以Google的搜索框为例。您有以下框的html代码:

<input type="text" name="q" value="" id="searchText" maxlength="256"/>

使用上面的html代码形成selenium元素定位器的方式将是这样的..:

driver.find_element(:xpath, "//input[@name='q']")

然而,这还不够,正如我在上面的描述中所注意到的那样;你试过设置隐式等待。实现所需效果的更好方法是......

!30.times { if (driver.find_element(:xpath, "//input[@name='q']") rescue false) then break else sleep 1; end }

上面的代码将尝试30次,在每次尝试到达所需元素之前等待1秒(如果失败,您可以通过使用begin / rescue子句来捕获错误:

begin
   .    
   .
 [your code]
   .
   .
!30.times { if (driver.find_element(:xpath, "//input[@name='q']") rescue false) then break else sleep 1; end }    
   .
   .
driver.find_element(:xpath, "//input[@name='q']") #so you cause an error that u can capture
   .
   .
rescue    
puts "A time-out error occurred or you have used an invalid element locator"
   .
   .
end

希望有所帮助!