我有一个登入页面(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属性找到要控制的元素,但若是我将范例页面的表单代码改成:
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
这两天净回答跟 DOM 有关的问题了 ... 也不知道是为什么 ...
额 ... 以上是题外话 ... 你这个问题 ... 如果你都能写到这里了 ... 再继续一步应该不是什么难事 ...
编程除了需要严谨 ... 有时候在遇到不知道的问题时也需要天马行空的想象力和不怕碰壁的尝试 ...
<?php /* just copied six lines below ... */ $Browser = new COM('InternetExplorer.Application'); $Browserhandle = $Browser->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();事实上你可以处理网页上的所有元素 ... 都是这一个道理 ...
恩恩 ... 就是这样啦 ...