无法在selenium java

时间:2017-09-13 06:57:01

标签: java selenium selenium-webdriver testng

我能够在selenium java 3.4.0和geckodriver 0.16上执行我的脚本,但是由于新的更新,一些函数被弃用,因为我不得不更改我的浏览器配置代码,现在它不是完全执行。它不执行整个脚本。

代码之前(在升级到java 3.5.3之前):

  System.setProperty("webdriver.firefox.marionette", "C:\\geckodriver.exe");
        FirefoxProfile profile = new FirefoxProfile();

        profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
                "application/octet-stream;application/csv;text/csv;application/vnd.ms-excel;");
        profile.setPreference("browser.helperApps.alwaysAsk.force", false);
        profile.setPreference("browser.download.manager.showWhenStarting", false);
        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.download.dir", prodDownloadPath);
        driver = new FirefoxDriver(profile);
        driver.manage().window().maximize();

        driver.manage().timeouts().implicitlyWait(160, TimeUnit.SECONDS);

        driver.get(productionUrl);
        driver.findElement(By.linkText("Demand Summary")).click();
        Thread.sleep(2000);
        driver.findElement(
                By.xpath("//table[@class='TextObject']//tr//td[contains(text(),'16 Weeks Historical Trend')]")).click();
        Thread.sleep(2000);
        WebElement imageUrl = driver.findElement(By.xpath(".//*[@class='QvFrame Document_CH69']/div[2]/div[2]/img"));
        Actions oAction = new Actions(driver);
        oAction.moveToElement(imageUrl);
        oAction.contextClick(imageUrl).build().perform();
        driver.findElement(By.linkText("Send to Excel")).click();
        Thread.sleep(2000); 

最新代码(升级至3.5.3后):

System.setProperty("webdriver.firefox.marionette", "C:\\geckodriver.exe");
        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
                "application/octet-stream;application/csv;text/csv;application/vnd.ms-excel;");
        profile.setPreference("browser.helperApps.alwaysAsk.force", false);
        profile.setPreference("browser.download.manager.showWhenStarting", false);
        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.download.dir", prodDownloadPath);
        DesiredCapabilities dc = DesiredCapabilities.firefox();
        dc.setCapability(FirefoxDriver.PROFILE, profile);
        dc.setCapability("marionette", true);
        driver = new FirefoxDriver(dc);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(160, TimeUnit.SECONDS);
        driver.get(productionUrl);
        driver.findElement(By.linkText("Demand Summary")).click();
        Thread.sleep(2000);
        driver.findElement(
                By.xpath("//table[@class='TextObject']//tr//td[contains(text(),'16 Weeks Historical Trend')]")).click();
        Thread.sleep(2000);
        WebElement imageUrl = driver.findElement(By.xpath(".//*[@class='QvFrame Document_CH80']/div[2]/div[2]/img"));
        Actions oAction = new Actions(driver);
        oAction.moveToElement(imageUrl);
        oAction.contextClick(imageUrl).build().perform();
        driver.findElement(By.linkText("Send to Excel")).click();
        Thread.sleep(1000);

以前的版本:

-Selenium Java 3.4.0  
-Selenium Server Standalone 3.4  
-Gecko 0.16  
-FF 46.0    

最新版本:

-Selenium Java 3.5.3  
-Selenium Server Standalone 3.5.3  
-Gecko 0.18  
-FF 55.0.3    

我在执行脚本期间遇到org.openqa.selenium.ElementNotInteractableException:异常。我应该使用哪些版本的组合?或者我需要更改我的代码或其他什么?请帮忙。

1 个答案:

答案 0 :(得分:0)

使用Implicit wait

driver.get("https://stackoverflow.com/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

explicit wait

WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));

供参考

WebDriverWait Wait= new WebDriverWait(driver,20);
Wait.until(ExpectedConditions.elementToBeClickable (By.id("Locator")));