2015-04-06 51 views
1

下一個div元素我的html代碼:試圖找到從當前

<div style="display: inline-block; position: absolute; top: 84px; bottom: auto; left: 5px; right: auto; width: auto; height: auto; max-width: none; max-height: none;" _afrc="2 1 8 1 start top">**CRED ID:** </div> 
<div style="display: inline-block; position: absolute; top: 84px; bottom: auto; left: 115px; right: auto; width: auto; height: auto; max-width: none; max-height: none;" _afrc="3 1 8 1 start top">**11111972**</div> 
<div style="display: inline-block; position: absolute; top: 103px; bottom: auto; left: 5px; right: auto; width: auto; height: auto; max-width: none; max-height: none;" _afrc="2 1 10 1 start top">**CAQH ID:** </div> 
<div style="display: inline-block; position: absolute; top: 103px; bottom: auto; left: 115px; right: auto; width: auto; height: auto; max-width: none; max-height: none;" _afrc="3 1 10 1 start top">**11189685**</div> 

如果事情顯示了你兩顆星它應該是大膽的。我將文字加粗,使其顯示效果更好。無論如何,我可以使用//div[contains(text(),'CRED ID:')]獲取包含「CRED ID」的div元素。實際的cred ID值是「11111972」。我想知道,當我使用上面的xpath時,我可以告訴它得到下一個div的文本值(即11111972)。我不能真正使用../div,因爲在這個級別有更早和更晚的div。任何人都可以建議

我可以使用@FindBy並創建一個列表,找到索引如果「CRED ID」並添加1我想,但它似乎應該有另一種方式?

謝謝

p.s.我從來沒有真正理解跟隨兄弟姐妹,但我不知道這是否會在這裏工作?

回答

1

可以使用following-sibling軸:

WebElement credId = driver.findElement(By.xpath("//div[contains(text(),'CRED ID:')]")); 
WebElement credIdValue = credId.findElement(By.xpath("following-sibling::div")); 

或者,你可以一次過實現它:

WebElement credIdValue = credId.findElement(By.xpath("//div[contains(text(),'CRED ID:')]/following-sibling::div")); 
+0

感謝。你認爲這會更有效率嗎?或者做@FindBy並製作一個列表,然後遍歷列表直到找到文本,然後我們找到下一個元素? – Tony

+0

@Tony我只是通過xpath根據提議的文本獲取元素。 – alecxe