2016-04-21 15 views
-1

我正嘗試在QwebView瀏覽器中使用javascript顯示選定的文本。但我不知道爲什麼它不工作.. 這裏是我的代碼: -在Qwebview中使用javascript顯示選定的文本

frame = self.page().mainFrame().documentElement() 
frame.evaluateJavaScript("alert("+"'"+frame.getSelection().toString()+"'"+");") 

當我運行這段代碼就說明我的錯誤: -

Traceback (most recent call last): 
File "GUI-Test.py", line 32, in slotshowxpath 
frame.evaluateJavaScript("alert("+"'"+frame.getSelection().toString()+"'"+");") 
AttributeError: 'QWebElement' object has no attribute 'getSelection' 

回答

0

僅僅因爲你的名字的變量frame

frame = self.page().mainFrame().documentElement() 

並不意味着它是一個QWebFrame:

Each QWebPage object contains at least one frame, the main frame, obtained using QWebPage.mainFrame().

http://pyqt.sourceforge.net/Docs/PyQt4/qwebframe.html

A QWebElement object allows easy access to the document model, represented by a tree-like structure of DOM elements. The root of the tree is called the document element and can be accessed using QWebFrame.documentElement().

http://pyqt.sourceforge.net/Docs/PyQt4/qwebelement.html

如果您在QWebElement class看,有沒有名爲getSelection()方法。

+0

我知道,但我想知道如何獲得使用JavaScript的選定元素。我也知道我們可以使用self.selectedText獲取文本,但是我正在尋找元素。 –

+0

@rickyrana,'frame.evaluateJavaScript(「alert(window.getSelection()。toString())」)' – 7stud

+0

感謝兄弟,它的工作 –

相關問題