我有一個登入頁面(http://**.com/def.php)頁的表單代碼范例:<form action="abc.php" method="POST" name="f_action">
<input type="text" name="id">
<input type="password" name="pwd">
<input type="submit" value="OK" name="action">
</form>我藉由PHP呼叫COM “InternetExplorer.Application”去填寫表單并登入,代碼如下:<?php
$Browser = new COM('InternetExplorer.Application');
$Browserhandle = $Browser->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屬性找到要控制的元素,但若是我將范例頁面的表單代碼改成:<form action="abc.php" method="POST">將「name="f_action"」改成「無Name屬性」,這樣php代碼要如何改寫才能同樣可以讓COM “InternetExplorer.Application”填寫表單并登入?感謝!
1 回答

開心每一天1111
TA貢獻1836條經驗 獲得超13個贊
你這個問題 ... 如果你都能寫到這里了 ... 再繼續一步應該不是什么難事 ...
編程除了需要嚴謹 ... 有時候在遇到不知道的問題時也需要天馬行空的想象力和不怕碰壁的嘗試 ...
<?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();
事實上你可以處理網頁上的所有元素 ... 都是這一個道理 ...
恩恩 ... 就是這樣啦 ...
- 1 回答
- 0 關注
- 109 瀏覽
添加回答
舉報
0/150
提交
取消