0

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')}  

有关信息,wait 是一种自定义方法:

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 采用更新的源文件。

似乎情况并非如此,因为......出现了与前一个相同的错误。

有人有想法吗?

4

1 回答 1

1

这是一个 selenium 错误,无论是因为您的 webdriver 在到达元素之前超时,还是因为您指定了错误的选择器(即 --> 您收到错误元素未找到),您都会得到它。如果我是你,我会使用 xpath 作为选择器,因为它似乎是最可靠的定位器。让我们以谷歌的搜索框作为一个元素为例。您有以下用于该框的 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

希望有帮助!

于 2012-08-10T12:06:48.167 回答