2015-02-24 49 views
0

我使用貝哈特3.0和1.6水貂。貝哈特/水貂 - 無法找到與XPath元素與goutteDriver

那些代碼Selenium2和殭屍的工作,但不能與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();  

有誰知道發生了什麼事?

回答

0

你嘗試findById()

例如

/** 
* @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(); 
} 

例如,

/** 
* 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)); 
    } 
} 
+0

你好!你的解決方案會很好,但正如你所看到的,我的元素是在一張桌子裏面,沒有ID ... – nitche 2015-02-24 16:09:33

+0

@nitche - 請參閱'edit'部分。 – BentCoder 2015-02-24 16:32:13