Can Selenium interact with an existing browser session?

后端 未结 11 889
野性不改
野性不改 2020-11-22 08:47

Does anybody know if Selenium (WebDriver preferably) is able to communicate with and act through a browser that is already running before launching a Selenium Client?

11条回答
  •  花落未央
    2020-11-22 09:10

    This is pretty easy using the JavaScript selenium-webdriver client:

    First, make sure you have a WebDriver server running. For example, download ChromeDriver, then run chromedriver --port=9515.

    Second, create the driver like this:

    var driver = new webdriver.Builder()
       .withCapabilities(webdriver.Capabilities.chrome())
       .usingServer('http://localhost:9515')  // <- this
       .build();
    

    Here's a complete example:

    var webdriver = require('selenium-webdriver');

    var driver = new webdriver.Builder()
       .withCapabilities(webdriver.Capabilities.chrome())
       .usingServer('http://localhost:9515')
       .build();
    
    driver.get('http://www.google.com');
    driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');
    driver.findElement(webdriver.By.name('btnG')).click();
    driver.getTitle().then(function(title) {
       console.log(title);
     });
    
    driver.quit();
    

提交回复
热议问题