-1

i am trying to display the selected text in QwebView browser with javascript. but i dont know why its not working.. here is my code:-

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

when i am running this code it shows me the error:-

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'
ricky rana
  • 57
  • 6

1 Answers1

0

Just because you name a variable frame:

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

does not mean it's a 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

If you look in the QWebElement class, there's no method named getSelection().

7stud
  • 46,922
  • 14
  • 101
  • 127
  • i know that but i want to know that how can i get the selected element using javascript. i also know that we can get the text using self.selectedText but i am looking for the element. – ricky rana Apr 21 '16 at 10:12
  • @rickyrana, `frame.evaluateJavaScript("alert(window.getSelection().toString())”)` – 7stud Apr 21 '16 at 10:29