Php 使用selenium和xpath单击按钮

Php 使用selenium和xpath单击按钮,php,selenium,xpath,selenium-webdriver,Php,Selenium,Xpath,Selenium Webdriver,我正在尝试单击一个名为“添加新警报”的按钮: <div class="text-right"> <a class="btn btn-primary " data-ajaxcom="" href="/admin/person/59139/alerts/add"> <span class="glyphicon glyphicon-plus"></span> Add new alert </a> </div> 下面是我的代码

我正在尝试单击一个名为“添加新警报”的按钮:

<div class="text-right">
<a class="btn btn-primary " data-ajaxcom="" href="/admin/person/59139/alerts/add">
<span class="glyphicon glyphicon-plus"></span>
Add new alert
</a>
</div>

下面是我的代码:

    <?php

    // Load up the composer autoloader, which will allow us to use the composer
    // stuff
    require 'vendor/autoload.php';

    $driver = new \Behat\Mink\Driver\Selenium2Driver(
        'firefox', 'base_url'
    );

    // init session:
    $session = new \Behat\Mink\Session($driver);

    // start session:
    $session->start();

    $session->visit('http://www.mywebsite.com/admin');

    $session->wait(400 + rand(0, 100));
    $page = $session->getPage();
    $page->fillField('username', 'admin');
    $page->fillField('password', 'password');
    $page->pressButton('submit');

    $session->visit('http://www.mywebsite.com/admin/person/59139');
    function clickbtnwithxpath($xpath)
        {
            $session = $this->getSession();
            $element = $session->getPage()->find(
                '//a[contains(@class, "btn btn-primary ")]',
                $session->getSelectorsHandler()->selectorToXpath('//a[contains(@class, "btn btn-primary ")]', $xpath)
            );

            // error handling
            if (null === $element) {
                throw new \InvalidArgumentException(sprintf('Could not evaluate XPath: "%s"', $xpath));
            }

            // click button
            $element->click();

        }
?>


它让我登录并进入警报页面,但不会单击按钮。我是xpath的新手。我想这里肯定有错误。但我已经尝试过了。有人能帮我工作吗?

如果没有ID和特定于数据的类,我将依赖链接文本:

//a[contains(., "Add new alert")]

对不起,那不行。有人能帮我点击这个按钮吗用户名:@alecxe在我的原始脚本中没有生成错误,更改链接文本的xpath也不会生成错误。结果是一样的,我不能用我目前的php知识点击这个按钮