1

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.

4

3 回答 3

0

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

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

于 2018-07-09T19:03:30.373 回答
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");
        }
    }
于 2018-07-10T10:41:01.403 回答
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交互 。

您的主要问题是您使用的二进制文件版本之间的不兼容,如下所示:

  • 您的Selenium 客户端ChromeDriver版本没有被检测到。
  • 您的JDK 版本是非常古老的1.8.0_77 。

因此JDK v8u77与最新的Selenium Client v3.13.0ChromeDriver v2.40Chrome 浏览器 v67.0之间存在明显的不匹配

解决方案

于 2018-07-09T11:59:55.097 回答