如何使用Python 3和Selenium或JavaScript更改Web WhatsApp中的活动聊天

7

我正在使用Python通过Selenium控制Web版WhatsApp,我想知道是否可以更改当前活动聊天(即置顶聊天)。如果收到消息,聊天不会被设置为活动状态,它会一直保持在后台。

在JavaScript中,可以通过以下命令在控制台中查看所有聊天列表:

Store.chat.models

活动聊天存储在位置零,但将另一个聊天覆盖到位置零并不会使聊天变为活动状态。

我发现有一个名为"x_active"的变量,如果单击并查看聊天,则会将其更改为true(其他所有聊天都为false)。

例如:

Store.Chat.models[0].__x_active

然而,在Chrome控制台中将变量设置为true或false并不会改变UI中的任何内容,那么我该如何实现这样的行为呢?


有点不清楚。你是想自己把聊天移到顶部吗?还是想点击最上面的聊天?或者其他什么? - Tarun Lalwani
我想自己移动聊天窗口,但目前我的丑陋解决方法是点击最上面的聊天窗口来实现同样的效果(但会让我的鼠标离开原本的位置)。 - Kev1n91
即使您使用Selenium进行点击或聚焦操作时做得很好,鼠标也会被移动。如果您希望鼠标回到先前的位置,请保存该位置,执行单击操作,然后将鼠标设置回其位置。 - m3nda
3个回答

4
您需要使用Selenium来点击左侧窗格中的联系人以使其活动。您可以使用CSS选择器轻松找到要点击的位置,如下所示:
driver.find_element_by_css_selector('span[title="Contact Name"]').click()

为了轻松地识别哪些联系人有未读消息,您可以使用JavaScript Store.chat.models列表,并在列表中查找具有变量__x_hasUnread = True的对象,以及变量__x_formattedTitle的值来查找要用于以上查找的联系人姓名。


你在哪里找到了 'driver' 变量?它存储在哪里? - Renan Coelho
“driver” 将是您的 webdriver 变量。 - Breaks Software

3
在WhatsApp Web中,如果您进行检查,可以看到联系人姓名驻留在类名为'.chat'的div中。
您可以通过执行以下脚本来向WhatsApp Web中的左侧联系人添加监听器,该脚本应该在您的whatsapp_login函数中执行。以下是whatsapp_login函数:
def whatsapp-login(request):
    global driver

    profile = webdriver.FirefoxProfile()
    profile.accept_untrusted_certs = True
    driver = webdriver.Firefox(firefox_profile=profile)

    driver.get('https://web.whatsapp.com/')

    script_path = os.path.dirname(os.path.abspath(__file__))
    script = open(os.path.join(script_path, "add_eventlistener.js"), "r").read()

    driver.execute_script(script)

以下是使用MutationObserver的add_listener脚本:
var observer = new MutationObserver(function(mutations) {
            mutations.forEach(function(mutation) {
                if(mutation.attributeName === 'class') {

                    var attributeValue = $(mutation.target).prop(mutation.attributeName);
                    console.log("attributeValue: "+attributeValue);
                    if(attributeValue.indexOf('hover') > -1) {

                        var user = $(mutation.target).find('.chat-title').find('span').attr('title');
                        console.log('Class attribute changed to:', attributeValue);

                        $.ajax({
                            url: 'url of change active chat function',
                            type: "POST",
                            data: {"user": user},
                            headers: {"Access-Control-Allow-Origin": "*"},
                            success: function(data) {
                                console.log(data);
                            },
                            error: function(data) {
                                console.log(data);
                            }
                        });
                    }
                }
            });
        });

        Array.prototype.forEach.call(document.querySelectorAll('.chat'), function(element, index) {
            console.log(element);
            observer.observe(element, {
                attributes: true
            });
        });

在您的更改活动聊天功能中,您可以执行以下操作来更改活动聊天。在这里,您将通过Ajax调用获取用户,并遍历联系人列表:

def change_active_chat(user):
    recentList = driver.find_elements_by_xpath("//span[@class='emojitext ellipsify']")
        for head in recentList:
            if head.text == user:
                head.click()

head.click()会更改当前活跃的聊天。


仅供澄清:Javascript 代码存储在 add_eventlistener.js 文件中,是吗? - Kev1n91

0
然而,在Chrome控制台选项卡中设置变量为true或false并没有改变UI中的任何内容。您正在更改布尔值,但请求未被提交,因此您将看不到任何更改。我的建议是首先使用XPath或CSS或其他方便的定位技术单击webElement。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接