2016-06-02 10 views
1

시나리오-별로 찾기는 첫 번째 클래스와 함께 할 수

는 다음과 같은 여러 CSS 클래스와 함께 입력 필드가 말 :

<input class="form-control existing-user" value="Darth Darth Binks"/> 

그리고 behat의를 당신은 CSS 클래스에 의해 입력 요소를 찾아 그 값을 찾고 싶다.

$field = $this->getPage()->findAllBy('css', 'form-control')[0]->getValue(); 
// $field is now "Darth Darth Binks" 

이 질문

여기에 차이점은 무엇입니까

$field = $this->getPage()->findAll('css', 'existing-user')[0]->getValue(); 
// $field is now null 

작동하지 않습니다 작동? 먼저 작성되지 않은 수업 중 하나를 찾으면 특별한 것을해야합니까?

+0

[div 요소를 동일한 클래스 이름으로 반복하여 내용을 가져옵니다.] (http://www.inanzzz.com/index.php/post/vsok/iterate-div-elements-with-same-class-name-to - get-their-contents), [CSS 선택자로 ID 태그가있는 요소를 클릭하십시오] (http://www.inanzzz.com/index.php/post/2y0l/click-on-an-element-that -has-an-id-tag-with-css-selector), [ID가있는 요소의 콘텐츠 가져 오기] (http://www.inanzzz.com/index.php/post/97ju/get-the-content -of-given-element-with-id), [ID 태그를 가진 요소를 클릭하십시오] (http://www.inanzzz.com/index.php/post/pzxm/click-on-an-element- that-has-an-id-tag) – BentCoder

답변

1

두 예제 모두 유효한 CSS 선택기를 사용해야합니다. 클래스에는 '.'을 사용해야합니다. 수업 앞에서. 제공된 입력 요소에 대한 몇 가지 유효한 CSS 선택기는 다음과 같습니다

input.form-control 
 
input.existing-user 
 
input.form-control.existing-user

그래서 올바른 사용은 다음과 같습니다 또한

$field = $this->getPage()->findAll('css', '.existing-user')->getValue(); 

제발, 제발 명심 findall은 방법이없는 것 요소가 발견되지 않는 경우는 예외를 슬로우 해, 대신에 null를 돌려 주어, null로 get 값을 사용하면 (자) PHP의 치명적인 에러가 발생합니다. 예외 처리를 구현하고 필요한 경우 waitForElement 메서드를 사용/구현합니다.