[英]How to get text from web element where didn't have unic id or class in selenium java
我想問一下如何從沒有unic id / class的元素中獲取文本,我嘗試使用xpath(從網絡瀏覽器復制),但是這不起作用。
嘗試下面的代碼;
("//*[@class='col-sm-4']//*[@class='text-grey'][1]").getText();
這可能會有所幫助。
Document doc = Jsoup.parse(driver.getPageSource());
Elements content = doc.select("span[class^=text-grey]");
ArrayList<String> allTextIntextGreyClass = (ArrayList<String>) content.eachText();
然后,您可以使用ArrayList來獲取要使用的文本,也可以使用“內容”。 如果這不起作用,則可以獲取內部HTML並對其進行處理以獲得所需的正確上下文。 您可以按以下方式獲取innerHTML:
String inHTML = driver.findElement(By.className("text-grey")).getAttribute("innerHTML");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.