Skip to content
Advertisement

Why I can’t get the window title of Edge in python 3

Why can’t i get the window title of Microsoft Edge in Python 3

My coding environment: windows10(x86) python3.7

I want to use win32gui to get the active window title, but I met some problem when I opened Microsoft Edge.

Here is my code:

import win32gui as w
import time

while True:
    time.sleep(1)
    titleName = w.GetWindowText(w.GetForegroundWindow())
    print(titleName)

As my code said I want to get current active window title after each second.

It’s all OK before I opened Microsoft Edge.

When I openes edge, the programme showed an error.

question picture:
question picture

As the picture said:

The programme can Identify all active windows except Microsoft Edge.

When I opened Microsoft Edge, it shows:

Traceback (most recent call last):  
File "P:/weather/main.py", line 8, in <module> print(titleName)
UnicodeEncodeError: 'gbk' codec can't encode character 'u200b' in position 103: illegal multibyte sequence

It shows it is a UnicodeEncodeError, but there is no strange char in the title.

I don’t know why.

I’ve checked a lot on the internet, but none of can help me fixed it.

Please help me.

Thank you very much.

Advertisement

Answer

I test with your code and it works fine when opening Edge. I think the issue is related with the title of the website you open in Edge. In the error message, it points out character u200b, that’s the cause of the issue.

u200b is an invisible space. For more information, you can refer to this article: u200b: What it is, and why it messes up your code or data. You can try to remove u200b using regular expressions when getting the title. You can add this line of code before print the title name:

titleName.replace('u200b','')
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement