dongyakui8675 2016-05-26 11:46
浏览 41
已采纳

在PHP Mink中选择没有id的表单元素?

I'm experimenting with PHP Mink (installed as on nodejs cannot find module 'zombie' with PHP mink). I am trying to parse a webpage (which I have no control over), which has an element like this inside a form:

<input tabindex="5" value="Do Submit!" class="my_btn my_btn_2" type="submit"></input>

Notably, this <input> has neither id nor name, and so I cannot find a way to select this element.

I would rather avoid using XPath, because I wouldn't like to specifically specify a hierarchy path which can very well change in the future. I'd best like to look up a child inside the form, which has a value attribute with value Do Submit!, but I don't know how to specify that in Mink?

I created a minimal example demonstrating this; here is the HTML file:

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>

... and here is the PHP file:

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() ."
";
echo "  status code: " . $session->getStatusCode() ."
";

$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() ."
";

# try to get the button:
$myBtn = $myForm->findField("Do Submit!");
echo "button selected by value is: " . var_export($myBtn, true) ."
"; // NULL

$myBtn = $myForm->find('css', 'my_btn');
echo "button selected by css is: " . var_export($myBtn, true) ."
"; // NULL
?>

The script outputs:

$ php test_php_mink_selbtnnoid.php 
  current URL: http://localhost:8090/selbtnnoid.htm
  status code: 200
my input tag is: input
button selected by value is: NULL
button selected by css is: NULL

So, how can I select / get a reference to this button element in this HTML file?

  • 写回答

2条回答 默认 最新

  • dongyingtang3803 2016-05-26 16:00
    关注

    First you should identify a unique section if any, after that, identify your element. Here are some possible selectors:

    css: #my-form input[type=submit]
    css: #my-form input.my_btn
    css: #my-form input.my_btn_2
    
    xpath: //*[@id='my-form']//input[@type='submit']
    xpath: //*[@id='my-form']//input[contains(@class, 'my_btn')]

    if you have only one unique input in the page you can remove "my-form" part. You don't have to specify a hierarchy path in any case (xss, xpath), it may be a bad practice for automation. Also try to avoid using text in selectors if you know that it might change/translate.

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办