example.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. // An example of using php-webdriver.
  3. // Do not forget to run composer install before and also have Selenium server started and listening on port 4444.
  4. namespace Facebook\WebDriver;
  5. use Facebook\WebDriver\Remote\DesiredCapabilities;
  6. use Facebook\WebDriver\Remote\RemoteWebDriver;
  7. require_once('vendor/autoload.php');
  8. // start Chrome with 5 second timeout
  9. $host = 'http://localhost:4444/wd/hub'; // this is the default
  10. $capabilities = DesiredCapabilities::chrome();
  11. $driver = RemoteWebDriver::create($host, $capabilities, 5000);
  12. // navigate to 'http://www.seleniumhq.org/'
  13. $driver->get('https://www.seleniumhq.org/');
  14. // adding cookie
  15. $driver->manage()->deleteAllCookies();
  16. $cookie = new Cookie('cookie_name', 'cookie_value');
  17. $driver->manage()->addCookie($cookie);
  18. $cookies = $driver->manage()->getCookies();
  19. print_r($cookies);
  20. // click the link 'About'
  21. $link = $driver->findElement(
  22. WebDriverBy::id('menu_about')
  23. );
  24. $link->click();
  25. // wait until the page is loaded
  26. $driver->wait()->until(
  27. WebDriverExpectedCondition::titleContains('About')
  28. );
  29. // print the title of the current page
  30. echo "The title is '" . $driver->getTitle() . "'\n";
  31. // print the URI of the current page
  32. echo "The current URI is '" . $driver->getCurrentURL() . "'\n";
  33. // write 'php' in the search box
  34. $driver->findElement(WebDriverBy::id('q'))
  35. ->sendKeys('php') // fill the search box
  36. ->submit(); // submit the whole form
  37. // wait at most 10 seconds until at least one result is shown
  38. $driver->wait(10)->until(
  39. WebDriverExpectedCondition::presenceOfAllElementsLocatedBy(
  40. WebDriverBy::className('gsc-result')
  41. )
  42. );
  43. // close the browser
  44. $driver->quit();