回答
假設標籤看起來HTML是這樣的:
<span id="spannyspan" title="the Title of SPAN">The Text</span>
那麼WebElement將最好走近這樣的:
WebDriver driver = new FirefoxDriver();
WebElement theSpan = driver.findElement(By.id("spannyspan"));
String title = theSpan.getAttribute("title");
String label = theSpan.getText();
System.out.println(title); // will return "the Title of SPAN"
System.out.println(label); // will return "The Text"
// both without apostrophes ofcourse
如果這沒有幫助,請提供標籤的樣本HTML正嘗試獲取
由於我不知道 - 是最好的如何將非功能性HTML元素的WebElement作爲標籤? – 2012-06-29 08:40:37
如果您需要從跨度獲取文本,那麼是的。想象一下,跨度將保存訂單ID,這可以稍後對數據庫進行驗證... – 2012-06-29 11:35:54
我只是認爲可能有一個類似被動WebElement的東西與減少指令集 – 2012-06-29 11:45:11
Suppose there is a label contain text "hello":
<label>hello</label>
then what you have to do is:
# first find this element using xpath or css locator and store that into WebElement object:
WebElement label = driver.findElement(By.xpath("//label[contains(text(),'hello')]");
# After that using getText() method you can get the text of the label element.getText() method return value in string so,store the value in the string variable:
String lbltext = label.getText();
# After that if you want to print the value then
system.out.println("text = "+lbltext);
請考慮重新格式化您的答案。用這些怪異的粗體文本和未格式化的代碼片段閱讀是非常困難的。請閱讀[我如何寫出一個好答案](https://stackoverflow.com/help/how-to-answer)。 – Clijsters 2017-05-26 09:49:57
- 1. 使用硒webdriver找到標籤文本
- 2. 使用硒webdriver(xpath)讀取文本
- 3. 硒的webdriver用TestNG - 無法從標籤
- 4. 硒的webdriver只是img標籤
- 5. 硒的webdriver點擊鏈接標籤
- 6. 改變與硒的webdriver標籤在Firefox
- 7. 硒webdriver的標籤自動化驗證
- 8. 硒Webdriver鼠標
- 9. 使用硒的webdriver
- 10. 獲得使用硒嵌套div標籤的webdriver
- 11. 專注於最近打開的標籤使用硒webdriver
- 12. 如何使用硒webdriver提取多個Span標籤下的鏈接
- 13. 從使用硒標籤獲取文本
- 14. 使用硒的webdriver獲取超時
- 15. 獲取硒使用文本的webdriver
- 16. 使用硒webdriver切換回父母標籤
- 17. 如何使用硒webdriver獲得多個chekcbox標籤名稱?
- 18. Excel使用硒webdriver
- 19. 使用硒webdriver獲取DOCTYPE C#
- 20. 檢查使用硒的webdriver
- 21. 無法使用硒的webdriver
- 22. 如何使用硒的webdriver
- 23. 無法使用硒的webdriver
- 24. 如何使用硒的webdriver
- 25. 如何使用硒的webdriver
- 26. 如何使用硒的webdriver
- 27. 如何使用硒的webdriver
- 28. 如何使用硒的webdriver
- 29. 硒的webdriver:使用Javascript
- 30. 如何使用硒的webdriver
請提供HTML代碼,以便更容易回答你的問題 – 2012-10-11 04:09:55