突出显示元素的硒代码仅在系统之一上起作用。 我已经在两者上都更新了chrome和chrome驱动程序,但是在一台机器上可以工作,但是当试图突出显示页面元素时,代码在另一台机器上中断了。 以下是例外:
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)
....
在特定时间段和中断时间内找不到该元素。
答案 0 :(得分:0)
此错误消息...
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'
...表示 ChromeDriver 无法与 WebDriverWait 之后返回的 WebElement 进行交互。
您的主要问题是所使用的二进制版本之间的不兼容性:
因此 JDK v8u77 与最新的 Selenium Client v3.13.0 , ChromeDriver v2.40 和< em> Chrome浏览器v67.0
@Test
。答案 1 :(得分:0)
将代码包装在try
/ catch
子句中,并在catch
内保存webdriver.getPageSource()
的内容。然后检查其内容(如果使用扩展名.html
命名文件,甚至可以在浏览器中打开它),并查看是否确实存在带有id='body_x_grid_x__ctl2__ctl0'
的元素。
我不确定,但是该ID似乎是由应用程序自动生成的,在另一台计算机上生成的ID可能有所不同。
答案 2 :(得分:0)
您的问题不在于荧光笔,而在于您的webElement:
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)
增加等待时间,因为您现有的等待时间为15秒:根据您的网站页面加载的最长时间增加:
WebDriverWait wait = new WebDriverWait(driver, wait time in second);
WebElement we = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("your xpath")));
获取webElement之后,您可以使用以下代码查找荧光笔:
public void highLighter(WebDriver driver, WebElement we) {
try{
JavascriptExecutor js = (JavascriptExecutor)driver;
String var = (String) js.executeScript("return arguments[0].getAttribute('style', arguments[1]);", we);
js.executeScript("return arguments[0].setAttribute('style', arguments[1]);", we,"border:4px solid red;");
Thread.sleep(200);
js.executeScript("return arguments[0].setAttribute('style', arguments[1]);", we,var);
}catch(Exception e){
System.out.println("unable to HighLight");
}
}