>백엔드 개발 >PHP 튜토리얼 >html - 藉由PHP呼叫COM “InternetExplorer.Application”,如何不用name属性模拟用户登入?

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

WBOY
WBOY원래의
2016-06-06 20:50:461262검색

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


我藉由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属性找到要控制的元素,但若是我将范例页面的表单代码改成:


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

感谢!

回复内容:

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


我藉由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属性找到要控制的元素,但若是我将范例页面的表单代码改成:


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

感谢!

这两天净回答跟 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();

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

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

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.