【问题标题】:Click a button using selenium and xpath单击使用 selenium 和 xpath 的按钮
【发布时间】:2015-05-18 20:59:04
【问题描述】:

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

<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 的新手。我认为这里一定有错误。但是我已经尝试过了。有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: php selenium xpath selenium-webdriver


    【解决方案1】:

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

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

    【讨论】:

    • 对不起,这不起作用。有人可以帮我点击这个按钮
    • 用户名:@alecxe 在我的原始脚本中没有产生错误,更改链接文本的 xpath 也不会产生错误。结果是一样的,我无法使用我目前的 php 知识单击此按钮
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 2016-02-21
    • 2016-11-26
    • 1970-01-01
    • 2018-08-22
    • 2021-03-29
    相关资源
    最近更新 更多