2017-04-10 106 views
1

我正在尝试设置Web应用程序的测试 - 我在Docker和PHPUnit 5.7.19中拥有带有VNC的Selenium独立3.2服务器。PHPUnit抛出异常,当应该没有

然而一行

$this->webDriver->findElement(WebDriverBy::xpath($main_button_xpath))->click(); 

有时会抛出(但有时不)一个异常时,它不应该 - 我已经通过VNC看着它和目标按钮被看见并且是存在在屏幕上它仍然抛出异常。自从我从2.57升级Selenium服务器后,这些错误开始出现。

你有什么想法如何修复或更好地调试吗?

非常感谢您

编辑:异常

The element reference is stale. Either the element is no longer attached to the DOM or the page has been refreshed. 
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html 
Build info: version: '3.2.0', revision: '8c03df6', time: '2017-03-02 09:34:51 -0800' 
System info: host: 'dbcbaf52ae71', ip: '172.17.0.3', os.name: 'Linux', os.arch: 'amd64', os.version: '3.10.0-514.10.2.el7.x86_64', java.version: '1.8.0_121' 
Driver info: org.openqa.selenium.firefox.FirefoxDriver 
Capabilities [{moz:profile=/tmp/rust_mozprofile.JUP1ydgC8zCR, rotatable=false, timeouts={implicit=0, page load=300000, script=30000}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=52.0, platformVersion=3.10.0-514.10.2.el7.x86_64, moz:processID=17210, browserName=firefox, platformName=linux}] 
Session ID: 47bf2157-c7a8-47ea-a82a-3ab3ddd0ee61 
+0

您是否有实际异常消息和堆栈跟踪的示例? – segFault

+0

@segFault添加了异常 –

回答

0

你尝试做水木清华这样吗?

$wait = new \Facebook\WebDriver\WebDriverWait($driver, 5, 250); 
$condition = \Facebook\WebDriver\WebDriverExpectedCondition::presenceOfElementLocated(Facebook\WebDriver\WebDriverBy::xpath($main_button_xpath)); 

$btn = $wait->until($condition); 
$btn->click(); 

UPDATE:

进场做反复的尝试可能看起来像水木清华下面公布。我仍然相信这只是可能帮助取决于页面上发生了什么。

$limit = 20; // might be different 
$staleException = NULL; 

for($i = 1; $i <= 20; $i++){ 
    try{ 
     // assuming $webDriver is ready to use and $main_button_xpath, too 
     $webDriver->findElement(WebDriverBy::xpath($main_button_xpath))->click(); 
     $staleException = NULL; 
     break; 
    } catch (\Facebook\WebDriver\Exception\StaleElementReferenceException $ex) { 
     $staleException = $ex; 
     usleep(250000); // 250 milliseconds wait 
    } 
} 

if($staleException){ 
    throw $staleException; 
} 

,并回答您的评论:是的,我认为,没有妥善解决它的不便干净整洁的代码感。为了避免这种类型的事情,我们假设对这些有点低级的细节了解页面上的html和js,以了解选择哪种防御策略。我不认为除了针对每一个棘手的事情(或棘手的事物类型)发生的一系列策略之外,还有其他的“通用解决方案”。

+0

会尝试,但为什么会发生这种情况?它在3.2 –

+0

的更新之前完美运行,看到了关于异常细节的更新。我想“等待”能力不会解决问题。看起来它确实找到了这个元素,但之后就失去了它。页面上的任何复杂的JS? – xmike

+0

是的,这是一个用PHP和JS编写的CRM软件 –