首页 > 解决方案 > Selenium highlight working only on one specific machine

问题描述

Selenium code to highlight an element is working only on one of the system. I have updated chrome and chrome driver on both of them but in one machine, it works but code breaks on another machine when trying to highlight the page element. Below is the exception:

An error occurred while fetching element : Expected condition failed: waiting for visibility of element located by By.id: body_x_grid_x__ctl2__ctl0 (tried for 15 second(s) with 500 MILLISECONDS interval)
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: '**', ip: '**', os.name: 'Windows Server 2008 R2', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_77'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab), userDataDir=**}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=67.0.3396.99, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=false, acceptInsecureCerts=false, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=}]
Session ID: 9854688adcdbe56519b9869c496b58e2
    at com.selenium.element.action.Wait.elementAction(Wait.java:68)
....

It does not find the element within specific periods and breaks.

标签: seleniumselenium-webdriverwebdriverselenium-chromedriverwebdriverwait

解决方案


将您的代码包装在try/catch子句中,并在其中catch保存webdriver.getPageSource(). 然后检查它的内容(如果你用扩展名命名文件,你甚至可以在浏览器中打开它.html)并查看一个元素是否id='body_x_grid_x__ctl2__ctl0'实际存在。

我不确定,但看起来这个 id 是由应用程序自动生成的,并且在另一台机器上生成的方式可能有所不同。


推荐阅读