2013-09-02 44 views
12

Tôi đang cố gắng để có xpath tìm một số div và xác minh rằng div có một văn bản cụ thể là string.Selenium và xpath: tìm một div có lớp/id và xác minh văn bản bên trong

Đây là HTML:

<div class="Caption"> 
    Model saved 
</div> 

<div id="alertLabel" class="gwt-HTML sfnStandardLeftMargin sfnStandardRightMargin sfnStandardTopMargin"> 
    Save to server successful 
</div> 

Đây là mã Tôi đang sử dụng tại thời điểm này:

viewerHelper_.getWebDriver().findElement(By.xpath("//div[contains(@class, 'Caption' and .//text()='Model saved']")); 
viewerHelper_.getWebDriver().findElement(By.xpath("//div[@id='alertLabel'] and .//text()='Save to server successful']")); 

Cụ thể:

//div[contains(@class, 'Caption' and .//text()='Model saved'] 
//div[@id='alertLabel'] and .//text()='Save to server successful'] 

Trả lời

24

Để xác minh điều này: -

<div class="Caption"> 
    Model saved 
</div> 

Viết này -

//div[contains(@class, 'Caption') and text()='Model saved'] 

Và để xác minh điều này: -

<div id="alertLabel" class="gwt-HTML sfnStandardLeftMargin sfnStandardRightMargin sfnStandardTopMargin"> 
    Save to server successful 
</div> 

Viết này -

//div[@id='alertLabel' and text()='Save to server successful'] 
+0

Chỉ cần tò mò, là có một cách để đạt được mục tiêu tương tự như trên sử dụng 'findElement (By.className ("") '? Nếu vậy, văn bản sẽ ở đâu ified đi? Cảm ơn. – Siva

4

Để chiếm hàng đầu và dấu khoảng trắng, bạn có thể muốn sử dụng normalize-space()

//div[contains(@class, 'Caption') and normalize-space(.)='Model saved'] 

//div[@id='alertLabel' and normalize-space(.)='Save to server successful'] 

Lưu ý rằng //div[contains(@class, 'Caption') and normalize-space(.//text())='Model saved'] cũng làm việc.

0

Đối với lớp và văn bản -

// div [chứa (@ lớp, 'Phụ đề') và (text(), 'mẫu lưu')]

Đối với lớp và id -

// div [chứa (@ lớp, 'gwt-HTML') và @ id = "alertLabel"]

+0

Xpath đầu tiên không hợp lệ (được kiểm tra bằng FirePath) – Andrejs

Các vấn đề liên quan