How to choose value from an option list using PyQt4.QtWebKit

限于喜欢 提交于 2019-12-04 09:05:11

The QWebKit classes use CSS2 selector syntax to find elements.

So the required option could be found like this:

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

and then the selected attribute can be set on the option element like this:

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

However, for some reason, this does not immediately update the page (and nor does calling webview.reload()).

So if you need an immediate update, a better way might be to get the select element:

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

and then set the selected option like so:

select.evaluateJavaScript('this.selectedIndex = 3')

I did it this way :

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!