How to select a value from a list of options using PyQt4.QtWebKit

I am trying to automatically select a birthday from the list of options on my site using PyQt4.QtWebKit, but I am having problems with this.

When I want to select a switch, I do this:

doc = webview.page().mainFrame().documentElement() g = doc.findFirst("input[id=gender]") g.setAttribute("checked", "true") 

Or set some text input:

 doc = webview.page().mainFrame().documentElement() s = doc.findFirst("input[id=say_something]") s.setAttribute("value", "Say Hello To My Little Friends") 

But how to choose a month from this list of options?

 <select tabindex="11" name="birthday_m"> <option value="">---</option> <option value="1">JAN</option> <option value="2">FEB</option> <option value="3">MAR</option> </select> 
+4
source share
2 answers

QWebKit classes use CSS2 selector syntax to search for elements.

So, the required parameter can be found as follows:

 doc = webview.page().mainFrame().documentElement() option = doc.findFirst('select[name="birthday_m"] > option[value="3"]') 

and then the selected attribute can be set in the option element as follows:

 option.setAttribute('selected', 'true') 

However, for some reason, this does not immediately refresh the page (and does not call the webview.reload() call).

So, if you need an immediate update, the select element might be the best way:

 doc = webview.page().mainFrame().documentElement() select = doc.findFirst('select[name="birthday_m"]') 

and then set the selected parameter as follows:

 select.evaluateJavaScript('this.selectedIndex = 3') 
+6
source

I did it as follows:

 doc.evaluateJavaScript('document.getElementsByName("birthdate_m")[0].options[3].selected = true') 

If you have any suggestions on how to improve it, let me know.

+1
source

Source: https://habr.com/ru/post/1448109/


All Articles