社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

Python:wx。html2.WebView-无法使用JavaScript将第一个元素集中在页面加载上

Adam • 3 年前 • 532 次点击  

在Python中,使用wxPython Phoenix 4.1.1 wx。html2模块的WebView,当页面加载时,我希望键盘焦点落在页面上的第一个元素上,这样我就可以使用JAWS或NVDA屏幕读取器使用向下箭头键从一开始就开始阅读页面。但我无法做到这一点,因为在页面加载时,屏幕阅读器键盘焦点总是跳转到页面中间的某个位置。我已经尝试使用JavaScript手动将焦点设置为第一个 <h1> 使用 RunScript 在中调用的函数 wx.html2.EVT_WEBVIEW_LOADED 事件处理程序,如下所示,但这根本没有效果。

请注意,在这段代码中,我使用的是 the trick described here 显示WebView组件后,将屏幕读取器焦点移到WebView组件中。

class HelpHTMLDialog(wx.Dialog):
  def __init__(self, title, parent = None):
    super(HelpHTMLDialog, self).__init__(parent = parent, title = title)
    
    self.addBrowser()
    
    self.SetSize((900, 700))
    self.Centre()
    self.ShowModal()
    
  # Adds the web browser to this dialog and binds the page load event.
  def addBrowser(self):
    vbox = wx.BoxSizer(wx.VERTICAL)
    
    self.browser = wx.html2.WebView.New(self)
    self.Bind(wx.html2.EVT_WEBVIEW_LOADED, self.onPageLoad, self.browser)
    html = self.getHTML()
    self.browser.SetPage(html, '')
    
    vbox.Add(self.browser, 1, wx.EXPAND, 10)
    self.SetSizer(vbox)
    
  # Handles the WebView page load.
  def onPageLoad(self, event):
    # Clicks on the left upper corner of the page to move screen reader focus to the page
    self.browser.SetFocus()
    position = self.browser.GetPosition()
    position = self.browser.ClientToScreen(position)
    robot = wx.UIActionSimulator()
    robot.MouseMove(position)
    robot.MouseClick()
    
    # Move the keyboard focus to the first <h1> heading on the page
    self.browser.RunScript('''
var first = document.getElementsByTagName('h1')[0];
first.tabIndex = -1;
first.focus();
// I know this code runs after page load without a problem, because I tried to put alert('test') here, and it worked.
''')
    
  # Gets the HTML of the page.
  def getHTML(self):
    html = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>Hello Python</h1>
<h2>First heading</h2>
<p>Here is some text just to demonstrate the use of wx.html2.WebView in Python.</p>
<ul>
<li>Firs item.</li>
<li>Second item.</li>
<li>Third item.</li>
<li>Fourth item.</li>
</ul>
<h2>Second heading</h2>
<p>Final paragraph is here just to make the page a bit longer.</p>
<script>
// I have also tried putting the JavaScript code for focusing the first <h1> element here instead, but with no success either.
</script>
</body>
</html>
'''
    return html

那么,您对如何在页面加载后以我描述的方式有效控制屏幕阅读器键盘焦点有何想法?

谢谢

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/136460
 
532 次点击