【问题标题】:Selenium highlight working only on one specific machineSelenium 高亮仅在一台特定机器上工作
【发布时间】:2018-07-09 10:30:40
【问题描述】:

用于突出显示元素的 Selenium 代码仅适用于其中一个系统。 我已经更新了它们两个上的 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)
....

它没有找到特定周期和中断内的元素。

【问题讨论】:

    标签: selenium selenium-webdriver webdriver selenium-chromedriver webdriverwait


    【解决方案1】:

    此错误消息...

    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 ClientChromeDriver 版本未重新检测到。
    • 您的 JDK 版本1.8.0_77,相当古老。

    所以 JDK v8u77 和最新的 Selenium Client v3.13.0ChromeDriver v2.40 em>Chrome 浏览器 v67.0

    解决方案

    【讨论】:

    • Java、Selenium 和 chrome 浏览器升级到最新版本后仍然无法使用。
    【解决方案2】:

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

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

    【讨论】:

    • 检查过了。它存在于此页面上。此问题与系统相关,但无法找到 RC
    【解决方案3】:

    您的问题不在于荧光笔,而在于您的 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");
            }
        }
    

    【讨论】:

    • 相同的代码在同一网络上运行的不同机器上运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 2014-06-03
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    相关资源
    最近更新 更多