简体   繁体   English

如何使用Python 3通过selenium或javascript更改web whatsapp中的活动聊天

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

I am steering web whatsapp via selenium via Python, i was wondering if it is possible to change the active (top chat. If a message is recieved, the chat won't be set as active, it will always remain in the background. 我通过Python通过selenium引导web whatsapp,我想知道是否可以更改活动(顶级聊天。如果收到消息,聊天将不会被设置为活动,它将始终保留在后台。

In Javascript, one can see a list of all chat in the consoles via: 在Javascript中,可以通过以下方式查看控制台中所有聊天的列表:

Store.chat.models

The active chat is stored at position zero, however overwriting the position zero with another chat will not make the chat active. 活动聊天存储在零位置,但是通过另一次聊天覆盖位置零将不会使聊天处于活动状态。

I have found out that there is a variable called " x_active " which changes if a chat is clicked on and view to true (and all others have it as false). 我发现有一个名为“ x_active ”的变量,如果点击聊天并查看为true(其他所有人都将其视为false), x_active更改。

eg: 例如:

Store.Chat.models[0].__x_active

However setting the variable or true or false in the chrome Console tab did not change anything in the UI, so how can I achieve such behaviour? 但是,在Chrome控制台选项卡中设置变量或true或false并未更改UI中的任何内容,因此如何实现此类行为?

You'll need to use selenium to click on the contact in the left pane to make it active. 您需要使用selenium单击左窗格中的联系人以使其处于活动状态。 You can easily find the place to click using the CSS selector like this: 您可以使用CSS选择器轻松找到要单击的位置,如下所示:

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

To easily identify which contacts have unread messages, you can use your JavaScript Store.chat.models list, and find objects in the list with the variable __x_hasUnread = True, and the value of the variable __x_formattedTitle to find the contact name to use for the find above. 要轻松识别哪些联系人有未读消息,您可以使用JavaScript Store.chat.models列表,并使用变量__x_hasUnread = True查找列表中的对象,并使用变量__x_formattedTitle的值查找要用于找到上面。

in whatsapp web if you inspect you can see that contact name resides in div with class '.chat'. 在whatsapp web中,如果你检查,你可以看到联系人名称位于div中,类为“.chat”。

you can add listener to contacts in left side in whatsapp web by executing the following script in your whatsapp_login function. 您可以通过在whatsapp_login函数中执行以下脚本,在whatsapp web中向左侧的联系人添加侦听器。 Following is the whatsapp_login function: 以下是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)

following is the add_listener script which uses MutationObserver : 以下是使用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
            });
        });

inside your change active chat function you can do the following to change active chat. 在您的更改活动聊天功能中,您可以执行以下操作来更改活动聊天。 Here you will get the user from ajax call and iterate through the contact list: 在这里,您将从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() will change the active chat. head.click()将更改活动聊天。

However setting the variable or true or false in the chrome Console tab did not change anything in the UI 但是,在Chrome控制台选项卡中设置变量或true或false并未更改UI中的任何内容

You are changing the Boolean value but the request is not submitted as a result you wont see any change. 您正在更改布尔值,但未提交请求,因此您不会看到任何更改。 My suggestion is to Click on the webElement using Xpath or CSS or any other convenient locator techniques first. 我的建议是首先使用Xpath或CSS或任何其他方便的定位器技术单击webElement。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM