如何通过 selenium 或 javascript 与 Python 更改网络 whatsapp 中的活动聊天 3

How to change the active chat in web whatsapp via selenium or javascript with Python 3

我正在通过 Python 通过 selenium 引导网络 whatsapp,我想知道是否可以更改活动(顶部聊天。如果收到消息,聊天将不会设置为活动,它会一直留在后台。

在 Javascript 中,可以通过以下方式查看控制台中所有聊天的列表:

Store.chat.models

活动聊天存储在位置零,但是用另一个聊天覆盖位置零不会使聊天处于活动状态。

我发现有一个名为“x_active”的变量,如果单击聊天并将其查看为真(而所有其他人都将其设为假),该变量会发生变化。

例如:

Store.Chat.models[0].__x_active

然而,在 chrome 控制台选项卡中设置变量或 true 或 false 并没有改变 UI 中的任何内容,那么我怎样才能实现这种行为?

您需要使用 selenium 单击左侧窗格中的联系人以将其激活。您可以像这样使用 CSS 选择器轻松找到要单击的位置:

driver.find_element_by_css_selector('span[title="Contact Name"]').click()

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

在 whatsapp web 中,如果您进行检查,您可以看到联系人姓名位于 div 和 class '.chat'。

您可以通过在您的 whatsapp_login 函数中执行以下脚本,在 whatsapp web 的左侧为联系人添加侦听器。以下是 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() 将更改活动聊天。

However setting the variable or true or false in the chrome Console tab did not change anything in the UI

您正在更改布尔值,但请求未提交,因此您看不到任何更改。 我的建议是首先使用 Xpath 或 CSS 或任何其他方便的定位器技术单击 webElement。