2014-01-19 18 views
0

我得到一個錯誤從代碼的最後一行下面使用Selenium從腳本標記獲取javaScript代碼:這可能嗎?

// Scala code 
val driver: HtmlUnitDriver = new HtmlUnitDriver() 
val el = driver.findElementByXPath(xp) 
val js = el.getText() 

返回的「厄爾尼諾」元素是「腳本」標籤。我在想,如果有可能還是無法得到使用.getText()

在這裏,標籤的內容真實錯誤

Exception in thread "main" java.util.NoSuchElementException: None.get 

回答

0

至於我可以看到,這是不可能訪問的內容一個使用XPath的「腳本」元素。我發現這個問題的解決方案是在HTML源上應用正則表達式:

// Scala code 
val regex = "your (pattern)".r 
val res = (regex findFirstMatchIn driver.getPageSource()).get group 1 
2

是美國可以通過使用屬性,這樣el.g​​etAttribute(「的innerHTML」)得到一個腳本的內容;

相關問題