在Selenium中自动关闭Web浏览器

时间:2017-01-03 16:50:09

标签: java selenium webdriver drivers

我目前正在尝试重新运行旧的Selenium测试。它按预期工作,但现在当我尝试运行它时,我收到一个错误。以下是代码:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class IMDBSearch {

public static void main(String[] args)
{
    //new driver for chrome
    WebDriver driver = new ChromeDriver();
    //entering the url
    String url = "http://www.imdb.com/search/title";
    driver.get(url);
    //clicking the featured film radio button
    WebElement featureFilm = driver.findElement(By.id("title_type-1"));
    featureFilm.click();
    //clicking the tvmovie radio button
    WebElement tvMovie = driver.findElement(By.id("title_type-2"));
    tvMovie.click();
    //clicking the tv radio button
    WebElement tvButton = driver.findElement(By.id("title_type-3"));
    tvButton.click();
    //using the xpath to select a 7.0 minimum rating
    WebElement userRating = driver.findElement(By.xpath("//body//div[@id='main']//div/select[@name='user_rating-min']/option[text()= '7.0']"));
    userRating.click();
    //using xpath to select US as a country
    WebElement country = driver.findElement(By.xpath("//body//div[@id='main']//div/select[@class = 'countries']/option[@value= 'us']"));
    country.click();
    //using xpath to select the sorting by user rating
    WebElement sort = driver.findElement(By.xpath("//body//div[@id= 'main']//div/select[@name = 'sort']/option[@value = 'user_rating,asc']"));
    sort.click();
    //using xpath to click on the search button
    WebElement searchButton = driver.findElement(By.xpath("//div[@id= 'main']/p/button[text()= 'Search']"));
    searchButton.click();
    //using xpath to get the first movie from the list
    //displaying the first movie from the list
    WebElement firstTitle = driver.findElement(By.xpath("//div[@id = 'main']//h3/span[text()= '1.']/following-sibling::a"));
    String title = firstTitle.getText();
    System.out.println("1. " + title);
    //using xpath to get the second movie from the list
    //displaying the second movie from the list
    WebElement secondTitle = driver.findElement(By.xpath("//div[@id = 'main']//h3/span[text()= '2.']/following-sibling::a"));
    String title2 = secondTitle.getText();
    System.out.println("2. " + title2);
    //using xpath to get the third movie from the list
    //displaying the third movie from the list
    WebElement thirdTitle = driver.findElement(By.xpath("//div[@id = 'main']//h3/span[text()= '3.']/following-sibling::a"));
    String title3 = thirdTitle.getText();
    System.out.println("3. " + title3);
    //using xpath to get the fourth movie from the list
    //displaying the fourth movie from the list
    WebElement fourthTitle = driver.findElement(By.xpath("//div[@id = 'main']//h3/span[text()= '4.']/following-sibling::a"));
    String title4 = fourthTitle.getText();
    System.out.println("4. " + title4);
    //using xpath to get the fifth movie from the list
    //displaying the fifth movie from the list
    WebElement fifthTitle = driver.findElement(By.xpath("//div[@id = 'main']//h3/span[text()= '5.']/following-sibling::a"));
    String title5 = fifthTitle.getText();
    System.out.println("5. " + title5); 

}

虽然我之前知道这是有效的,但它一直在向我提出这个错误:

Starting ChromeDriver 2.23.409699 (49b0fa931cda1caad0ae15b7d1b68004acd05129)         on port 20969
Only local connections are allowed.
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException:      session not created exception
from unknown error: Runtime.executionContextCreated has invalid 'context':   {"auxData":  {"frameId":"3940.1","isDefault":true},"id":1,"name":"","origin":"://"}
  (Session info: chrome=55.0.2883.87)
  (Driver info: chromedriver=2.23.409699   (49b0fa931cda1caad0ae15b7d1b68004acd05129),platform=Windows NT 6.1.7601 SP1  x86_64)     (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 2.14 seconds
Build info: version: '3.0.0-beta2', revision: '2aa21c1', time: '2016-08-02 15:03:28 -0700'
System info: host: 'Qualitest-PC', ip: '192.168.2.106', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_101'
Driver info: org.openqa.selenium.chrome.ChromeDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at  org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:683)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:247)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:130)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:143)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:170)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:159)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:116)
at PracticeClass.main(PracticeClass.java:10)

1 个答案:

答案 0 :(得分:0)

您应该尝试更新您的chromedriver版本。您使用的版本(v2.23)仅针对v51-53指定,并且您使用的是Chrome 55.尝试使用最新的chromedriver v2.27