3

So the basic spiel, "I am new to this thing" called Selenium using Facebook's PHP Webdriver (https://github.com/facebook/php-webdriver) and I cannot find anything that matches "How to open a new tab in chrome" using this particular technology. Any help will be very much appreciated.

A virtual cup of coffee to you, kind programmer!

Doug
  • 3,312
  • 1
  • 24
  • 31
Jp Serame
  • 101
  • 1
  • 8

4 Answers4

5

New way to do it since php-webdriver 1.10.0:

// Default behavior, without specifying window type
$driver->switchTo()->newWindow();

// Open new window
$driver->switchTo()->newWindow(WebDriverTargetLocator::WINDOW_TYPE_WINDOW);
// Open new tab
$driver->switchTo()->newWindow(WebDriverTargetLocator::WINDOW_TYPE_TAB);

$driver->switchTo()->window($driver->getWindowHandles()[1]);

See doucmentation for more examples.

Ondrej Machulda
  • 998
  • 1
  • 12
  • 24
miguelik
  • 475
  • 4
  • 11
2

You can go with JS solution like:

$webdriver->executeScript("window.open('". $url ."','_blank');", array());
Yevgen
  • 1,239
  • 3
  • 15
  • 30
0

Something like this should work:

$driver = RemoteWebDriver::create($host, DesiredCapabilities::chrome());
$driver->get('http://mine.com');

// Via driver: http://facebook.github.io/php-webdriver/classes/RemoteWebDriver.html#property_keyboard

$kbd = $driver->getKeyboard();
$kbd->sendKeys(WebDriverKeys.CONTROL,'t');

// Via an element: http://facebook.github.io/php-webdriver/classes/WebDriverElement.html#method_sendKeys

$element = $driver->findElement(WebDriverBy::id('somethingOnScreen'));
$element->sendKeys(WebDriverKeys.CONTROL,'t');
Andrew Regan
  • 5,087
  • 6
  • 37
  • 73
  • Thanks for the answer, but no it didn't work. Still opens a new window, not a new tab. I have these: `$host = 'http://localhost:4444/wd/hub'; $driver = RemoteWebDriver::create($host, DesiredCapabilities::chrome()); $driver->get($url); $kbd = $driver->getKeyboard(); $kbd->sendKeys(WebDriverKeys.CONTROL,'t'); $uname = $driver->findElement( WebDriverBy::xpath('/html/body/div[5]/div/div[1]/div[2]/div/form/input[1]') );` – Jp Serame Feb 14 '16 at 13:15
  • Just to clarify: the `get` call creates one window, the Ctrl-T creates another window (i.e. it definitely does something)? – Andrew Regan Feb 14 '16 at 13:20
  • I need to check what you're seeing, as it's generally accepted that Ctrl-T (or Cmd-T for Mac) will - if it works at all - open a new tab, see: http://stackoverflow.com/a/19441842/954442 – Andrew Regan Feb 14 '16 at 13:23
  • Hey there again @Andrew. Even omitting the sendkeys part will open the new window. However, the opening a new tab didnt work at all. and yes, i have read about the source you pointed at yesterday. Anyways, thanks for the help, really appreciate it! – Jp Serame Feb 15 '16 at 14:04
  • My actual problem is that I have this client-server setup, that when I launched an automation site from the client, the new window opens at the server and not on the client. Do you have suggestions on these? Im trying to comprehend selenium grid. am i on the right track? – Jp Serame Feb 15 '16 at 14:10
0
$driver->getKeyboard()->sendKeys(
  array(WebDriverKeys::CONTROL, 't'),
);