Skip to content

Instantly share code, notes, and snippets.

@ledudu
Created August 30, 2013 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ledudu/6392701 to your computer and use it in GitHub Desktop.
Save ledudu/6392701 to your computer and use it in GitHub Desktop.
<?php
/**
* V2EX每日奖励自动领取脚本 - PHP / Selenium版
*
* 使用前需要准备的:
* Oracle Java Runtime - http://java.com/
* Selenium Server - http://seleniumhq.org/
* PHP - http://php.net/
* PHP-WebDriver - https://github.com/Element-34/php-webdriver
* Firefox - http://mozilla.org/
*
* 配置说明:
* 1. 安装Java, Firefox
* 2. 使用以下命令运行 Selenium Server:
* java -jar selenium-server-standalone-2.32.0.jar
* 3. 修改下方的 $username, $password, $wd_host
* $username - V2EX 用户名
* $password - V2EX 密码
* $wd_host - 运行Selenium Server电脑的IP/路径 (一般情况下只需要修改IP部分)
* 4. 将 PHP-WebDriver 里的 PHPWebDriver 文件夹下载复制到 index.php 同目录里
* 5. 用你喜欢的方式运行 index.php
*
* 其他说明:
* 该脚本完全没考虑容错,反正找不到相应的链接,60秒后就超时直接扔exception了。
*/
require_once 'PHPWebDriver\__init__.php';
$username = '';
$password = '';
$wd_host = 'http://127.0.0.1:4444/wd/hub';
$WebDriver = new PHPWebDriver_WebDriver($wd_host);
$session = $WebDriver->session('firefox');
$session->open('http://v2ex.com/');
wait_element("//a[@href='/signin']");
click("//a[@href='/signin']");
wait_element("//input[@type='submit']");
type("//input[@name='u']", $username);
type("//input[@name='p']", $password);
click("//input[@type='submit']");
wait_element("//a[@href='/mission/daily']");
click("//a[@href='/mission/daily']");
wait_element("//input[@type='button']");
click("//input[@type='button']");
wait_element("//a[@href='/balance']");
$session->close();
function present_element($session, $argv) {
return count($session->elements('xpath', $argv['xpath']));
}
function wait_element($element) {
global $session;
echo "Wait element: {$element}\r\n";
$wait = new PHPWebDriver_WebDriverWait($session, 60, 1, array('xpath' => $element));
$result = $wait->until('present_element');
if ($result == 0) {
die("Find element {$element} failed.");
}
}
function select_value($element, $value) {
global $session;
echo "Select {$element} -> {$value}\r\n";
$select = new PHPWebDriver_Support_WebDriverSelect($session->element('xpath', $element));
$select->select_by_value($value);
}
function type($element, $str) {
global $session;
echo "Type {$element} -> {$str}\r\n";
$session->element('xpath', $element)->sendKeys($str);
}
function click($element) {
global $session;
echo "Click {$element}\r\n";
$session->element('xpath', $element)->click();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment