Behat/Mink - не могу найти элемент по xpath с goutteDriver

Я использую Behat 3.0 и Mink 1.6.

Эти коды работают с Selenium2 и Zombie, но не с Goutte:

    $this->assertSession()->elementTextContains('xpath', "//div[@id='pagecontent-shop']/form/table/tbody/tr[12]/td[2]", $arg1);

    $page = $this->getSession()->getPage();
    $element = $page->find('xpath', "//div[@id='pagecontent-shop']/form/table/tbody/tr[12]/td[2]",)->getText();       

Кто-нибудь знает, что происходит?


person nitche    schedule 24.02.2015    source источник


Ответы (1)


Вы пробовали findById()?

e.g.

/**
 * @When /^I click an element with ID "([^"]*)"$/
 *
 * @param $id
 * @throws \Exception
 */
public function iClickAnElementWithId($id)
{
    $element = $this->getSession()->getPage()->findById($id);
    if (null === $element) {
        throw new \Exception(sprintf('Could not evaluate element with ID: "%s"', $id));
    }
    $element->click();
}

e.g.

/**
 * Click on the element with the provided css id
 *
 * @Then /^I click on the element with id "([^"]*)"$/
 *
 * @param $elementId ID attribute of an element
 * @throws \InvalidArgumentException
 */
public function clickElementWithGivenId($elementId)
{
    $session = $this->getSession();
    $page = $session->getPage();

    $element = $page->find('css', '#' . $elementId);

    if (null === $element) {
        throw new \InvalidArgumentException(sprintf('Could not evaluate CSS: "%s"', $elementId));
    }

    $element->click();
}

РЕДАКТИРОВАТЬ: Пример ниже проходит через input элементов в таблице и находит те, у которых есть определенные name. Однако вам нужно немного изменить его.

/**
 * @Given /^the table contains "([^"]*)"$/
 */
public function theTableContains($arg1)
{
    $session = $this->getSession();
    $element = $session->getPage()->findAll('css', 'input');

    if (null === $element) {
        throw new \Exception(sprintf('Could not evaluate find select table'));
    }

    $options = explode(',', $arg1);
    $match = "element_name";

    $found = 0;
    foreach ($element as $inputs) {
        if ($inputs->hasAttribute('name')) {
            if (preg_match('/^'.$match.'(.*)/', $inputs->getAttribute('name')) !== false) {
                if (in_array($inputs->getValue(), $options)) {
                    $found++;
                }
            }
        }
    }

    if (intval($found) != intval(count($options))) {
        throw new \Exception(sprintf('I only found %i element in the table', $found));
    }
}
person BentCoder    schedule 24.02.2015
comment
Привет! ваше решение было бы отличным, но, как видите, мой элемент находится внутри таблицы и не имеет идентификатора... - person nitche; 24.02.2015