私はPHP Minkを試しています(nodejs cannot find module 'zombie' with PHP minkとしてインストールされています)。私は(私が制御することはできません)Webページを解析しようと、フォーム内でこのような要素を持っているのです:PHP Minkでidなしのフォーム要素を選択していますか?
<input tabindex="5" value="Do Submit!" class="my_btn my_btn_2" type="submit"></input>
は特に、この<input>
はid
もname
もない、と私は方法を見つけることができませんこの要素を選択します。
私は、XPathの使用を避けたいと思います。これは、将来的に非常にうまく変更できる階層パスを特に指定したくないからです。私はフォーム内の子を調べることをお勧めします。この子は、値Do Submit!
のvalue
属性を持っていますが、それをどのようにMinkで指定するのか分かりませんか?
これを示す最小限の例を作成しました。ここではHTMLファイルです:
selbtnnoid.htm
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
.my_form {
width: 50%;
border: 2px solid red;
}
.my_label {
font-size: large;
}
.my_input_2 {
font-size: medium;
}
.my_btn {
background-color: yellow;
}
.my_btn_2 {
font-size: large;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>Here is the form:</p>
<form method="post" action="wherever.php" id="my-form" class="my_form">
<h1>Some form here:</h1>
<p>
<label for="my-input-txt">
<span class="my_label">Some data:</span>
<input name="my-input-txt" id="my-input-txt" placeholder=" Enter data. " class="my_input_2" tabindex="1" type="text"></input>
</label>
</p>
<p>
<input tabindex="5" value="Do Submit!" class="my_btn my_btn_2" type="submit"></input>
</p>
</form>
</body>
</html>
は...と、ここでPHPファイルです:
test_php_mink_selbtnnoid.php
<?php
$nodeModPath = "/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules";
putenv("NODE_PATH=".$nodeModPath); # doesn't really help; use setNodeModulesPath
# composer autoload for mink:
require_once __DIR__ . '/vendor/autoload.php';
$zsrv = new \Behat\Mink\Driver\NodeJS\Server\ZombieServer();
$zsrv->setNodeModulesPath($nodeModPath . "/"); # needs to end with a trailing '/'
$driver = new \Behat\Mink\Driver\ZombieDriver($zsrv);
$session = new \Behat\Mink\Session($driver);
// start the session
$session->start();
//~ $session->visit('selbtnnoid.htm'); // nope; status code: 0 if just called locally
//~ $session->visit('file:///path/to/selbtnnoid.htm'); // nope; Error: listen EADDRINUSE 127.0.0.1:8124
$session->visit('http://localhost:8090/selbtnnoid.htm'); // run php -S localhost:8090 in the folder with these two files
echo " current URL: " . $session->getCurrentUrl() ."\n";
echo " status code: " . $session->getStatusCode() ."\n";
$page = $session->getPage();
$myForm = $page->findById("my-form");
$myInput = $myForm->findField("my-input-txt");
# check if we have the element: // yes, displays 'my input tag is: input'
echo "my input tag is: ". $myInput->getTagName() ."\n";
# try to get the button:
$myBtn = $myForm->findField("Do Submit!");
echo "button selected by value is: " . var_export($myBtn, true) ."\n"; // NULL
$myBtn = $myForm->find('css', 'my_btn');
echo "button selected by css is: " . var_export($myBtn, true) ."\n"; // NULL
?>
スクリプトの出力:
このHTMLファイルでこのボタン要素への参照を選択/取得するにはどうすればよいですか?
ありがとうございます。@lauda - 私は実際に他の一意の識別子を持っていないので、値のテキストは私が選択できる唯一のものです。クラスにセレクタが含まれていることに感謝していますが、それは役に立つかもしれません。 – sdbbs