当前位置:Gxlcms > PHP教程 > html-藉由PHP呼叫COM“InternetExplorer.Application”,如何不用name属性模拟用户登入?

html-藉由PHP呼叫COM“InternetExplorer.Application”,如何不用name属性模拟用户登入?

时间:2021-07-01 10:21:17 帮助过:8人阅读

我有一个登入页面(http://**.com/def.php)页的表单代码范例:

我藉由PHP呼叫COM “InternetExplorer.Application”去填写表单并登入,代码如下:

HWND;
$Browser->Visible = true;
$f1_url = "http://**.com/def.php";
$Browser->Navigate($f1_url);
sleep(5);
$Browser->Document->f_action->id->focus();
$Browser->Document->f_action->id->value = "username";
$Browser->Document->f_action->pwd->focus();
$Browser->Document->f_action->pwd->value = "password";
$Browser->Document->f_action->action->focus();
$Browser->Document->f_action->action->click();
$Browser->Quit();
?>

里面是藉由Name属性找到要控制的元素,但若是我将范例页面的表单代码改成:

我藉由PHP呼叫COM “InternetExplorer.Application”去填写表单并登入,代码如下:

HWND;
$Browser->Visible = true;
$f1_url = "http://**.com/def.php";
$Browser->Navigate($f1_url);
sleep(5);
$Browser->Document->f_action->id->focus();
$Browser->Document->f_action->id->value = "username";
$Browser->Document->f_action->pwd->focus();
$Browser->Document->f_action->pwd->value = "password";
$Browser->Document->f_action->action->focus();
$Browser->Document->f_action->action->click();
$Browser->Quit();
?>

里面是藉由Name属性找到要控制的元素,但若是我将范例页面的表单代码改成:

将「name="f_action"」改成「无Name属性」,这样php代码要如何改写才能同样可让COM “InternetExplorer.Application”填写表单并登入?

感谢!

这两天净回答跟 DOM 有关的问题了 ... 也不知道是为什么 ...

额 ... 以上是题外话 ... 你这个问题 ... 如果你都能写到这里了 ... 再继续一步应该不是什么难事 ...

编程除了需要严谨 ... 有时候在遇到不知道的问题时也需要天马行空的想象力和不怕碰壁的尝试 ...

HWND;
$Browser->Visible = true;
$f1_url = "http://**.com/def.php";
$Browser->Navigate($f1_url);
sleep(5);

/* $allforms is NOT an array ... it is an iterator ... */
$allforms = $Browser->Document->getElementsByTagName( 'form' );

/* current() is not implemented ... so we have to run a loop ... */
foreach( $allforms as $theform ) {

    /* i also copied these six lines and did some text replace work ... */
    $theform->id->focus();
    $theform->id->value = "username";
    $theform->pwd->focus();
    $theform->pwd->value = "password";
    $theform->action->focus();
    $theform->action->click();
    
}

/* bye browser ... have a nice day ... */
$Browser->Quit();

事实上你可以处理网页上的所有元素 ... 都是这一个道理 ...

恩恩 ... 就是这样啦 ...

人气教程排行