Use Selenium to simulate login and obtain page content

WBOY
Release: 2016-07-25 08:46:27
Original
1876 people have browsed it

Traditional cURL cannot execute browser scripts in the page, and when crawling some web pages that have restrictions on crawlers, it is often necessary to set detailed http headers to break through the restrictions, which is more complicated to write.

Introduction to Selenium:

Selenium is a tool for web application testing (and not just for testing).
Selenium runs directly in the browser, like a real user. Supports more browsers.

components

Selenium IDE: Firefox plug-in, with the function of recording scripts. Supports automatic recording of actions and automatic generation of automation scripts in other languages.

Selenium Remote Control (RC): supports multiple platforms (Windows, Linux) and multiple browsers (IE, Firefox, Opera, Safari, Chrome), and can be used in multiple languages ​​(Java, Ruby, Python, Perl, PHP, C# )Write use cases.

Selenium Grid: Allows Selenium-RC to scale for large test case sets or test case sets that need to be run in different environments.


Example: Drive chrome to simulate logging into Taobao and obtain page information

1. Go to the project homepage: SeleniumHQ download

Selenium Server (formerly the Selenium RC Server)

Third Party Browser Drivers NOT DEVELOPED by seleniumhq

(Select chrome driver)

Third Party Language Bindings NOT DEVELOPED by seleniumhq

(Choose PHP by Adam Goucher (SeHQ recommended php client))

2.Open selenium

  1. java -jar path_to_selenium.jar
  2. [-timeout 0]
  3. [-Dwebdriver.server.session.timeout=0]
  4. -Dwebdriver.chrome.driver="path_to_chrome_driver"
  5. -browser [-timeout=0] [ -browserTimeout=0]
  6. browserName=chrome,[timeout=0]
Copy code

If you need to run for a long time, please set the timeout period in each '[ ]' as appropriate

3.php code

  1. function waitForAjax() {
  2. global $session;
  3. do {
  4. sleep(1);
  5. } while($session->execute(array('script' => "return (document.readyState != 'complete')", 'args' => array())));
  6. } //This function will hang the script until Ajax ends
  7. require_once "webdriver/PHPWebDriver/__init__ .php";
  8. //Introducing selenium's PHP encapsulation function library
  9. // Download address: https://github.com/Element-34/php-webdriver
  10. // There are various methods of operating the browser in the document, such as obtaining All cookies etc.
  11. $wd_host = 'http://127.0.0.1:4444/wd/hub';
  12. $web_driver = new PHPWebDriver_WebDriver($wd_host);
  13. $session = $web_driver->session('chrome' );
  14. //Set the timeout period
  15. $session->implicitlyWait(5);
  16. $session->setScriptTimeout(5);
  17. $session->setPageLoadTimeout(15);
  18. //Open the connection
  19. $ session->open('http://login.m.taobao.com/login.htm?tpl_redirect_url=http://m.taobao.com');
  20. //Enter the verification code, if necessary
  21. sleep(5);
  22. //Please set the account password
  23. $session->element('css selector', 'input[name=TPL_username]')->value(array('value' => str_split ('your_username')));
  24. $session->element('css selector', 'input[name=TPL_password]')->value(array('value' => str_split('your_password')) );
  25. //Simulate clicking the login button
  26. $elements = $session->element('css selector', '.c-btn-oran-big')->click();
  27. //Open m .taobao.com, the cookie has been obtained at this time
  28. $session->open('http://m.taobao.com/');
  29. //Waiting for ajax to be loaded
  30. waitForAjax();
  31. $elements = $session->element('css selector', 'body')->text();
  32. //Get the page content when ajax is executed after logging in
  33. ?>
Copy the code

After that, you can perform various operations of the element method on the $session instance as needed.

Supports the following methods to select elements

ID xpath link text partial link text name tag name class name css selector
PS: How various libraries detect Ajax situations

jQuery: "jQuery.active"

Prototype: "Ajax.activeRequestCount"

Dojo: "dojo.io.XMLHTTPTransport.inFlight.length"

Login, Selenium


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template