Search code examples
javascriptpythonqwebviewqwebelement

How to click text area in Qwebview and python


I am using QWebView with python to create an automate application and I could select Qwebelements by tags and id, also it's easy to click buttons but I can't do the following - click elements with tag "Textarea" to prepare them to insert data - send keys to textrea elements

the code example for what I can do to fill a form is below but it doesn't work for textarea elements

page = webview.page().mainFrame()
doc = page.documentElement()
username = doc.findFirst("input[id=email]")
username.setAttribute('value', "[email protected]")
password = doc.findfirst("input[id=pass]")
bt.evaluateJavaScript('this.click()')

#this one after getting button element as above ones


Solution

  • Element focusing:

    You're trying to 'focus' an element, meaning that input goes to that element. to focus, just try this:

    txtarea = self.documentElement.findFirst('textarea[name="someTextArea"]')
    txtarea.evaluateJavascript('this.focus()')
    

    Setting Attributes vs Properties: The most straightforward way to do this is just set the value of the 'textarea' using JavaScript (instead of setting the attribute directly).

    Example:

    txtarea = self.documentElement.findFirst('textarea[name="someTextArea"]')
    txtarea.evaluateJavaScript('this.value="' + yourText + '";')
    

    Some extra details:

    The reason 'setAttribute' didn't work for you: you're trying to change the Attribute called 'value', whereas what you should actually change is the Property 'value'. the difference between the two is explained here.