Esempio n. 1
0
class Operation():
    def __init__(self, username, password):
        print '11'
        self.launcher = Launcher(username, password)
        print '22'
        self.driver = self.launcher.login()
        print '33'

    def publish(self, text):
        try:
            # 退出通知弹窗进入页面
            time.sleep(1)
            try:
                self.driver.find_element_by_xpath(
                    '//div[@class="_n8 _3qx uiLayer _3qw"]').click()
            except:
                pass
            try:
                self.driver.find_element_by_xpath(
                    '//textarea[@title="分享新鲜事"]').click()
                self.driver.find_element_by_xpath(
                    '//textarea[@title="分享新鲜事"]').send_keys(text)
            except:
                try:
                    self.driver.find_element_by_xpath(
                        '//textarea[@class="_3en1 _480e navigationFocus"]'
                    ).click()
                    self.driver.find_element_by_xpath(
                        '//textarea[@class="_3en1 _480e navigationFocus"]'
                    ).send_keys(text)
                except:
                    self.driver.find_element_by_xpath(
                        '//div[@class="_1mwp navigationFocus _395 _1mwq _4c_p _5bu_ _34nd _21mu _5yk1"]'
                    ).click()
                    self.driver.find_element_by_xpath(
                        '//div[@class="_1mwp navigationFocus _395 _1mwq _4c_p _5bu_ _34nd _21mu _5yk1"]'
                    ).send_keys(text)
            try:
                self.driver.find_element_by_xpath(
                    '//button[@class="_1mf7 _4jy0 _4jy3 _4jy1 _51sy selected _42ft"]'
                ).click()
            except:
                try:
                    self.driver.find_element_by_xpath(
                        '//button[@class="_42ft _4jy0 _ej1 _4jy3 _4jy1 selected _51sy"]'
                    ).click()
                except:
                    self.driver.find_element_by_xpath(
                        '//button[@data-testid="react-composer-post-button"]'
                    ).click()
            time.sleep(5)
            return [True, '']
        except Exception as e:
            return [False, e]
        finally:
            self.driver.close()

    def mention(self, username, text):
        try:
            # 退出通知弹窗进入页面
            time.sleep(1)
            try:
                self.driver.find_element_by_xpath(
                    '//div[@class="_n8 _3qx uiLayer _3qw"]').click()
            except:
                pass

            try:
                self.driver.find_element_by_xpath(
                    '//textarea[@title="分享新鲜事"]').click()
                self.driver.find_element_by_xpath(
                    '//textarea[@title="分享新鲜事"]').send_keys(text)
            except:
                self.driver.find_element_by_xpath(
                    '//div[@class="_1mwp navigationFocus _395 _1mwq _4c_p _5bu_ _34nd _21mu _5yk1"]'
                ).click()
                self.driver.find_element_by_xpath(
                    '//div[@class="_1mwp navigationFocus _395 _1mwq _4c_p _5bu_ _34nd _21mu _5yk1"]'
                ).send_keys(text)
            time.sleep(2)
            try:
                self.driver.find_element_by_xpath(
                    '//table[@class="uiGrid _51mz _5f0n"]/tbody/tr[3]/td[1]//a/div'
                ).click()
            except:
                self.driver.find_element_by_xpath(
                    '//table[@class="uiGrid _51mz _5f0n"]/tbody/tr[2]/td[2]//a/div'
                ).click()
            time.sleep(1)
            self.driver.find_element_by_xpath(
                '//input[@aria-label="你和谁一起?"]').send_keys(username)
            self.driver.find_element_by_xpath(
                '//input[@aria-label="你和谁一起?"]').send_keys(Keys.ENTER)
            time.sleep(1)
            try:
                self.driver.find_element_by_xpath(
                    '//button[@class="_1mf7 _4jy0 _4jy3 _4jy1 _51sy selected _42ft"]'
                ).click()
            except:
                self.driver.find_element_by_xpath(
                    '//button[@data-testid="react-composer-post-button"]'
                ).click()
            time.sleep(5)
            return [True, '']
        except Exception as e:
            return [False, e]
        finally:
            self.driver.close()

    def follow(self, uid):
        try:
            driver = self.launcher.target_page(uid)
            # 退出通知弹窗进入页面
            time.sleep(1)
            try:
                self.driver.find_element_by_xpath(
                    '//div[@class="_n8 _3qx uiLayer _3qw"]').click()
            except:
                pass
            try:
                driver.find_element_by_xpath(
                    '//button[@data-testid="page_profile_follow_button_test_id"]'
                ).click()
            except:
                driver.find_element_by_xpath(
                    '//div[@id="pagelet_timeline_profile_actions"]/div[2]/a[1]'
                ).click()
            time.sleep(5)
            return [True, '']
        except Exception as e:
            return [False, e]
        finally:
            driver.close()

    def not_follow(self, uid):
        try:
            driver = self.launcher.target_page(uid)
            # 退出通知弹窗进入页面
            time.sleep(1)
            try:
                self.driver.find_element_by_xpath(
                    '//div[@class="_n8 _3qx uiLayer _3qw"]').click()
            except:
                pass
            chain = ActionChains(driver)
            try:
                implement = driver.find_element_by_xpath(
                    '//div[@id="pagelet_timeline_profile_actions"]/div[2]/div[1]/div[1]'
                )
                chain.move_to_element(implement).perform()
                time.sleep(2)
                implement = driver.find_element_by_xpath(
                    '//div[@id="pagelet_timeline_profile_actions"]/div[2]/div[1]/div[1]'
                )
                chain.move_to_element(implement).perform()
                time.sleep(2)
                driver.find_element_by_xpath(
                    '//a[@ajaxify="/ajax/follow/unfollow_profile.php?profile_id=%s&location=1"]'
                    % uid).click()
            except:
                try:
                    implement = driver.find_element_by_xpath(
                        '//button[@data-testid="page_profile_follow_button_test_id"]'
                    )
                    chain.move_to_element(implement).perform()
                    time.sleep(2)
                    implement = driver.find_element_by_xpath(
                        '//button[@data-testid="page_profile_follow_button_test_id"]'
                    )
                    chain.move_to_element(implement).perform()
                    time.sleep(2)
                    driver.find_element_by_xpath(
                        '//a[@ajaxify="/ajax/follow/unfollow_profile.php?profile_id=%s&location=1"]'
                        % uid).click()
                except:
                    implement = driver.find_element_by_xpath(
                        '//button[@class="_42ft _4jy0 _3oz- _52-0 _4jy4 _517h _51sy"]'
                    )
                    chain.move_to_element(implement).perform()
                    time.sleep(2)
                    implement = driver.find_element_by_xpath(
                        '//button[@class="_42ft _4jy0 _3oz- _52-0 _4jy4 _517h _51sy"]'
                    )
                    chain.move_to_element(implement).perform()
                    time.sleep(2)
                    driver.find_element_by_xpath(
                        '//a[@ajaxify="/ajax/follow/unfollow_profile.php?profile_id=%s&location=1"]'
                        % uid).click()
            time.sleep(5)
            return [True, '']
        except Exception as e:
            return [False, e]
        finally:
            driver.close()

# 私信(未关注)

    def send_message(self, uid, text):
        #发送给未关注的用户
        try:
            driver = self.launcher.target_page(uid)
            message_url = 'https://www.facebook.com/messages/t/' + uid
            driver.get(message_url)
            time.sleep(5)
            # 退出通知弹窗进入页面
            time.sleep(1)
            try:
                self.driver.find_element_by_xpath(
                    '//div[@class="_n8 _3qx uiLayer _3qw"]').click()
            except:
                pass
            driver.find_element_by_xpath(
                '//div[@aria-label="输入消息..."]').send_keys(text)
            driver.find_element_by_xpath(
                '//div[@aria-label="输入消息..."]').send_keys(Keys.ENTER)
            time.sleep(5)
            return [True, '']
        except Exception as e:
            return [False, e]
        finally:
            driver.close()

# 私信(已关注)
# def send_message2(self, uid, text):
# 	#发送给已关注的用户
# 	try:
# 		driver = self.launcher.target_page(uid)
# 		url = driver.find_element_by_xpath('//a[@class="_51xa _2yfv _3y89"]/a[1]').get_attribute('href')
# 		driver.get('https://www.facebook.com' + url)
# 		time.sleep(4)
# 		driver.find_element_by_xpath('//div[@class="_1mf _1mj"]').click()
# 		driver.find_element_by_xpath('//div[@class="_1mf _1mj"]').send_keys(text)
# 		driver.find_element_by_xpath('//div[@class="_1mf _1mj"]').send_keys(Keys.ENTER)
# 	finally:
# 		driver.close()

# 点赞

    def like(self, uid, fid):
        try:
            post_url = 'https://www.facebook.com/' + uid + '/posts/' + fid
            video_url = 'https://www.facebook.com/' + uid + '/videos/' + fid
            self.driver.get(post_url)
            time.sleep(3)
            try:
                # 退出通知弹窗进入页面
                time.sleep(1)
                try:
                    self.driver.find_element_by_xpath(
                        '//div[@class="_n8 _3qx uiLayer _3qw"]').click()
                except:
                    pass
                self.driver.find_element_by_xpath(
                    '//div[@aria-label="Facebook 照片剧场模式"]')
                self.driver.get(video_url)
                time.sleep(2)
                # 退出通知弹窗进入页面
                time.sleep(1)
                try:
                    self.driver.find_element_by_xpath(
                        '//div[@class="_n8 _3qx uiLayer _3qw"]').click()
                except:
                    pass
                for each in self.driver.find_elements_by_xpath(
                        '//a[@data-testid="fb-ufi-likelink"]'):
                    try:
                        each.click()
                    except:
                        pass
            except:
                # 退出通知弹窗进入页面
                time.sleep(1)
                try:
                    self.driver.find_element_by_xpath(
                        '//div[@class="_n8 _3qx uiLayer _3qw"]').click()
                except:
                    pass
                for each in self.driver.find_elements_by_xpath(
                        '//a[@data-testid="fb-ufi-likelink"]'):
                    try:
                        each.click()
                    except:
                        pass
            time.sleep(5)
            return [True, '']
        except Exception as e:
            return [False, e]
        finally:
            self.driver.close()

# 评论

    def comment(self, uid, fid, text):
        try:
            post_url = 'https://www.facebook.com/' + uid + '/posts/' + fid
            video_url = 'https://www.facebook.com/' + uid + '/videos/' + fid
            self.driver.get(post_url)
            time.sleep(3)
            try:
                # 退出通知弹窗进入页面
                time.sleep(1)
                try:
                    self.driver.find_element_by_xpath(
                        '//div[@class="_n8 _3qx uiLayer _3qw"]').click()
                except:
                    pass
                self.driver.find_element_by_xpath(
                    '//div[@aria-label="Facebook 照片剧场模式"]')
                self.driver.get(video_url)
                # 退出通知弹窗进入页面
                time.sleep(1)
                try:
                    self.driver.find_element_by_xpath(
                        '//div[@class="_n8 _3qx uiLayer _3qw"]').click()
                except:
                    pass
                time.sleep(3)
                self.driver.find_element_by_xpath(
                    '//div[@class="UFICommentContainer"]/div/div').click()
                time.sleep(1)
                self.driver.find_element_by_xpath(
                    '//div[@class="notranslate _5rpu"]').click()
                time.sleep(1)
                self.driver.find_element_by_xpath(
                    '//div[@class="notranslate _5rpu"]').send_keys(text)
                time.sleep(1)
                self.driver.find_element_by_xpath(
                    '//div[@class="notranslate _5rpu"]').send_keys(keys.ENTER)
            except:
                # 退出通知弹窗进入页面
                time.sleep(1)
                try:
                    self.driver.find_element_by_xpath(
                        '//div[@class="_n8 _3qx uiLayer _3qw"]').click()
                except:
                    pass
                time.sleep(3)
                self.driver.find_element_by_xpath(
                    '//div[@class="UFICommentContainer"]/div/div').click()
                time.sleep(1)
                self.driver.find_element_by_xpath(
                    '//div[@class="notranslate _5rpu"]').click()
                time.sleep(1)
                self.driver.find_element_by_xpath(
                    '//div[@class="notranslate _5rpu"]').send_keys(text)
                time.sleep(1)
                self.driver.find_element_by_xpath(
                    '//div[@class="notranslate _5rpu"]').send_keys(keys.ENTER)
            time.sleep(5)
            return [True, '']
        except Exception as e:
            return [False, e]
        finally:
            time.sleep(3)
            self.driver.close()

# 分享

    def share(self, uid, fid, text):
        try:
            print 'uid, fid, text...', uid, fid, text
            post_url = 'https://www.facebook.com/' + uid + '/posts/' + fid
            video_url = 'https://www.facebook.com/' + uid + '/videos/' + fid
            self.driver.get(post_url)
            time.sleep(3)
            try:
                # 退出通知弹窗进入页面
                time.sleep(1)
                try:
                    self.driver.find_element_by_xpath(
                        '//div[@class="_n8 _3qx uiLayer _3qw"]').click()
                except:
                    pass
                self.driver.find_element_by_xpath(
                    '//div[@aria-label="Facebook 照片剧场模式"]')
                self.driver.get(video_url)
                time.sleep(1)
                # 退出通知弹窗进入页面
                time.sleep(1)
                try:
                    self.driver.find_element_by_xpath(
                        '//div[@class="_n8 _3qx uiLayer _3qw"]').click()
                except:
                    pass
                self.driver.find_element_by_xpath(
                    '//a[@title="发送给好友或发布到你的时间线上。"]').click()
                self.driver.find_element_by_xpath(
                    '//a[@title="发送给好友或发布到你的时间线上。"]').click()
                time.sleep(3)
                self.driver.find_element_by_xpath(
                    '//ul[@class="_54nf"]/li[2]').click()
                time.sleep(3)
                try:
                    self.driver.find_element_by_xpath(
                        '//div[@class="notranslate _5rpu"]').click()
                    time.sleep(1)
                    self.driver.find_element_by_xpath(
                        '//div[@class="notranslate _5rpu"]').send_keys(text)
                    time.sleep(1)
                except:
                    self.driver.find_element_by_xpath(
                        '//div[@class="_1mwp navigationFocus _395  _21mu _5yk1"]/div'
                    ).click()
                    time.sleep(1)
                    self.driver.find_element_by_xpath(
                        '//div[@class="_1mwp navigationFocus _395  _21mu _5yk1"]/div'
                    ).send_keys(text)
                    time.sleep(1)
                self.driver.find_element_by_xpath(
                    '//button[@data-testid="react_share_dialog_post_button"]'
                ).click()
            except:
                # 退出通知弹窗进入页面
                time.sleep(1)
                try:
                    self.driver.find_element_by_xpath(
                        '//div[@class="_n8 _3qx uiLayer _3qw"]').click()
                except:
                    pass
                self.driver.find_element_by_xpath(
                    '//a[@title="发送给好友或发布到你的时间线上。"]').click()
                self.driver.find_element_by_xpath(
                    '//a[@title="发送给好友或发布到你的时间线上。"]').click()
                time.sleep(5)
                self.driver.find_element_by_xpath(
                    '//ul[@class="_54nf"]/li[2]').click()
                time.sleep(5)
                try:
                    self.driver.find_element_by_xpath(
                        '//div[@class="notranslate _5rpu"]').click()
                    time.sleep(1)
                    self.driver.find_element_by_xpath(
                        '//div[@class="notranslate _5rpu"]').send_keys(text)
                    time.sleep(1)
                except:
                    self.driver.find_element_by_xpath(
                        '//div[@class="_1mwp navigationFocus _395  _21mu _5yk1"]/div'
                    ).click()
                    time.sleep(1)
                    self.driver.find_element_by_xpath(
                        '//div[@class="_1mwp navigationFocus _395  _21mu _5yk1"]/div'
                    ).send_keys(text)
                    time.sleep(1)
                self.driver.find_element_by_xpath(
                    '//button[@data-testid="react_share_dialog_post_button"]'
                ).click()
            time.sleep(5)
            return [True, '']
        except Exception as e:
            return [False, e]
        finally:
            self.driver.close()

#添加好友

    def add_friend(self, uid):
        try:
            driver = self.launcher.target_page(uid)
            driver.find_element_by_xpath(
                '//button[@class="_42ft _4jy0 FriendRequestAdd addButton _4jy4 _517h _9c6"]'
            ).click()
            time.sleep(5)
            return [True, '']
        except Exception as e:
            return [False, e]
        finally:
            driver.close()

#确认好友请求

    def confirm(self, uid):
        try:
            driver = self.launcher.target_page(uid)
            time.sleep(5)
            driver.find_element_by_xpath(
                '//div[@class="incomingButton"]/button').click()
            time.sleep(1)
            driver.find_element_by_xpath('//li[@data-label="确认"]/a').click()
            time.sleep(5)
            return [True, '']
        except Exception as e:
            return [False, e]
        finally:
            driver.close()


#删除好友

    def delete_friend(self, uid):
        try:
            driver = self.launcher.target_page(uid)
            time.sleep(1)
            driver.find_element_by_xpath(
                '//div[@id="pagelet_timeline_profile_actions"]/div/a').click()
            time.sleep(2)
            driver.find_element_by_xpath('//li[@data-label="删除好友"]/a').click()
            time.sleep(5)
            return [True, '']
        except Exception as e:
            return [False, e]
        finally:
            driver.close()
Esempio n. 2
0
class Operation():
    def __init__(self, username, password):
        self.launcher = Launcher(username, password)
        self.driver = self.launcher.login()

    def publish(self, text):
        time.sleep(1)
        try:
            self.driver.find_element_by_xpath(
                '//div[@class="_4bl9 _42n-"]').click()
        except:
            self.driver.find_element_by_xpath('//div[@class="_4bl9"]').click()
        time.sleep(2)
        try:
            self.driver.find_element_by_xpath(
                '//textarea[@title="分享新鲜事"]').click()
            self.driver.find_element_by_xpath(
                '//textarea[@title="分享新鲜事"]').send_keys(text)
        except:
            self.driver.find_element_by_xpath(
                '//div[@class="_1mwp navigationFocus _395 _1mwq _4c_p _5bu_ _34nd _21mu _5yk1"]'
            ).click()
            self.driver.find_element_by_xpath(
                '//div[@class="_1mwp navigationFocus _395 _1mwq _4c_p _5bu_ _34nd _21mu _5yk1"]'
            ).send_keys(text)
        try:
            self.driver.find_element_by_xpath(
                '//button[@class="_1mf7 _4jy0 _4jy3 _4jy1 _51sy selected _42ft"]'
            ).click()
        except:
            self.driver.find_element_by_xpath(
                '//button[@class="_42ft _4jy0 _ej1 _4jy3 _4jy1 selected _51sy"]'
            ).click()

    def mention(self, username, text):
        try:
            self.driver.find_element_by_xpath(
                '//div[@class="_4bl9 _42n-"]').click()
            time.sleep(3)
            self.driver.find_element_by_xpath(
                '//div[@class="_1mwp navigationFocus _395 _1mwq _4c_p _5bu_ _34nd _21mu _5yk1"]'
            ).send_keys(text)
            time.sleep(1)
            self.driver.find_element_by_xpath(
                '//table[@class="uiGrid _51mz _5f0n"]/tbody/tr[2]/td[2]/span/a/div'
            ).click()
            self.driver.find_element_by_xpath(
                '//input[@aria-label="你和谁一起?"]').send_keys(username)
            self.driver.find_element_by_xpath(
                '//input[@aria-label="你和谁一起?"]').send_keys(Keys.ENTER)
            time.sleep(1)
            self.driver.find_element_by_xpath(
                '//button[@class="_1mf7 _4jy0 _4jy3 _4jy1 _51sy selected _42ft"]'
            ).click()
        except Exception as e:
            print(e)

    def follow(self, uid):
        try:
            driver = self.launcher.target_page(uid)
            driver.find_element_by_xpath(
                '//a[@class="_42ft _4jy0 _4jy4 _517h _51sy"]').click()
        except Exception as e:
            print(e)

    def not_follow(self, uid):
        try:
            driver = self.launcher.target_page(uid)
            chain = ActionChains(driver)
            implement = driver.find_element_by_xpath(
                '//button[@class="_42ft _4jy0 _3oz- _52-0 _4jy4 _517h _51sy"]')
            chain.move_to_element(implement).perform()
            driver.find_element_by_xpath(
                '//a[@ajaxify="/ajax/follow/unfollow_profile.php?profile_id=100022934584514&location=1"]'
            ).click()
        except Exception as e:
            print(e)

# 私信(未关注)

    def send_message(self, uid, text):
        #发送给未关注的用户
        try:
            driver = self.launcher.target_page(uid)
            message_url = 'https://www.facebook.com/messages/t/' + uid
            driver.get(message_url)
            time.sleep(5)
            driver.find_element_by_xpath(
                '//div[@aria-label="输入消息..."]').send_keys(text)
            driver.find_element_by_xpath(
                '//div[@aria-label="输入消息..."]').send_keys(Keys.ENTER)
        except Exception as e:
            print(e)

# 私信(已关注)

    def send_message2(self, uid, text):
        #发送给已关注的用户
        try:
            driver = self.launcher.target_page(uid)
            url = driver.find_element_by_xpath(
                '//a[@class="_51xa _2yfv _3y89"]/a[1]').get_attribute('href')
            driver.get('https://www.facebook.com' + url)
            time.sleep(4)
            driver.find_element_by_xpath('//div[@class="_1mf _1mj"]').click()
            driver.find_element_by_xpath(
                '//div[@class="_1mf _1mj"]').send_keys(text)
            driver.find_element_by_xpath(
                '//div[@class="_1mf _1mj"]').send_keys(Keys.ENTER)
        except Exception as e:
            print(e)

#########

# 点赞

    def like(self, uid, fid):
        post_url = 'https://www.facebook.com/' + uid + '/posts/' + fid
        video_url = 'https://www.facebook.com/' + uid + '/videos/' + fid
        self.driver.get(post_url)
        time.sleep(5)
        try:
            self.driver.find_element_by_xpath(
                '//div[@aria-label="Facebook 照片剧场模式"]')
            self.driver.get(video_url)
            time.sleep(2)
            for each in self.driver.find_elements_by_xpath(
                    '//a[@data-testid="fb-ufi-likelink"]'):
                try:
                    each.click()
                except:
                    pass
        except:
            for each in self.driver.find_elements_by_xpath(
                    '//a[@data-testid="fb-ufi-likelink"]'):
                try:
                    each.click()
                except:
                    pass

# 评论

    def comment(self, uid, fid, text):
        post_url = 'https://www.facebook.com/' + uid + '/posts/' + fid
        video_url = 'https://www.facebook.com/' + uid + '/videos/' + fid
        self.driver.get(post_url)
        time.sleep(3)
        try:
            self.driver.find_element_by_xpath(
                '//div[@aria-label="Facebook 照片剧场模式"]')
            self.driver.get(video_url)
            time.sleep(3)
            self.driver.find_element_by_xpath(
                '//div[@class="UFICommentContainer"]/div/div').click()
            time.sleep(1)
            self.driver.find_element_by_xpath(
                '//div[@class="UFICommentContainer"]/div/div').send_keys(text)
            self.driver.find_element_by_xpath(
                '//div[@class="UFICommentContainer"]/div/div').send_keys(
                    Keys.ENTER)
        except:
            self.driver.find_element_by_xpath(
                '//div[@class="UFICommentContainer"]/div/div').click()
            time.sleep(1)
            self.driver.find_element_by_xpath(
                '//div[@class="UFICommentContainer"]/div/div').send_keys(text)
            self.driver.find_element_by_xpath(
                '//div[@class="UFICommentContainer"]/div/div').send_keys(
                    Keys.ENTER)

# 分享

    def share(self, uid, fid, text):
        print 'uid, fid, text...', uid, fid, text
        post_url = 'https://www.facebook.com/' + uid + '/posts/' + fid
        video_url = 'https://www.facebook.com/' + uid + '/videos/' + fid
        self.driver.get(post_url)
        time.sleep(3)
        try:
            self.driver.find_element_by_xpath(
                '//div[@aria-label="Facebook 照片剧场模式"]')
            self.driver.get(video_url)
            time.sleep(1)
            self.driver.find_element_by_xpath(
                '//a[@title="发送给好友或发布到你的时间线上。"]').click()
            self.driver.find_element_by_xpath(
                '//a[@title="发送给好友或发布到你的时间线上。"]').click()
            time.sleep(3)
            self.driver.find_element_by_xpath(
                '//ul[@class="_54nf"]/li[2]').click()
            time.sleep(3)
            try:
                self.driver.find_element_by_xpath(
                    '//div[@class="_1mwp navigationFocus _395  _21mu _5yk1"]/div'
                ).click()
                time.sleep(1)
                self.driver.find_element_by_xpath(
                    '//div[@class="_1mwp navigationFocus _395  _21mu _5yk1"]/div'
                ).send_keys(text)
                time.sleep(1)
            except:
                self.driver.find_element_by_xpath(
                    '//div[@class="notranslate _5rpu"]').click()
                time.sleep(1)
                self.driver.find_element_by_xpath(
                    '//div[@class="notranslate _5rpu"]').send_keys(text)
                time.sleep(1)
            self.driver.find_element_by_xpath(
                '//button[@data-testid="react_share_dialog_post_button"]'
            ).click()
        except:
            self.driver.find_element_by_xpath(
                '//a[@title="发送给好友或发布到你的时间线上。"]').click()
            self.driver.find_element_by_xpath(
                '//a[@title="发送给好友或发布到你的时间线上。"]').click()
            time.sleep(5)
            self.driver.find_element_by_xpath(
                '//ul[@class="_54nf"]/li[2]').click()
            time.sleep(5)
            try:
                self.driver.find_element_by_xpath(
                    '//div[@class="_1mwp navigationFocus _395  _21mu _5yk1"]/div'
                ).click()
                time.sleep(1)
                self.driver.find_element_by_xpath(
                    '//div[@class="_1mwp navigationFocus _395  _21mu _5yk1"]/div'
                ).send_keys(text)
                time.sleep(1)
            except:
                self.driver.find_element_by_xpath(
                    '//div[@class="notranslate _5rpu"]').click()
                time.sleep(1)
                self.driver.find_element_by_xpath(
                    '//div[@class="notranslate _5rpu"]').send_keys(text)
                time.sleep(1)
            self.driver.find_element_by_xpath(
                '//button[@data-testid="react_share_dialog_post_button"]'
            ).click()

#添加好友

    def add_friend(self, uid):
        try:
            driver = self.launcher.target_page(uid)
            driver.find_element_by_xpath(
                '//button[@class="_42ft _4jy0 FriendRequestAdd addButton _4jy4 _517h _9c6"]'
            ).click()
        except Exception as e:
            print(e)

#确认好友请求

    def confirm(self, uid):
        try:
            driver = self.launcher.target_page(uid)
            time.sleep(5)
            driver.find_element_by_xpath(
                '//div[@class="incomingButton"]/button').click()
            time.sleep(1)
            driver.find_element_by_xpath('//li[@data-label="确认"]/a').click()
        except Exception, e:
            print e
Esempio n. 3
0
class Operation():
	def __init__(self):
		self.launcher = Launcher('18538728360','zyxing,0513')
		self.driver = self.launcher.login()

	def publish(self):
		self.driver.find_element_by_xpath('//div[@class="_4bl9 _42n-"]').click()
		time.sleep(2)
		self.driver.find_element_by_xpath('//div[@class="_1mwp navigationFocus _395 _1mwq _4c_p _5bu_ _34nd _21mu _5yk1"]').send_keys('测试消息3')
		self.driver.find_element_by_xpath('//button[@class="_1mf7 _4jy0 _4jy3 _4jy1 _51sy selected _42ft"]').click()

	def mention(self):
		self.driver.find_element_by_xpath('//div[@class="_4bl9 _42n-"]').click()
		time.sleep(3)
		self.driver.find_element_by_xpath('//div[@class="_1mwp navigationFocus _395 _1mwq _4c_p _5bu_ _34nd _21mu _5yk1"]').send_keys('测试消息4')
		time.sleep(1)
		self.driver.find_element_by_xpath('//table[@class="uiGrid _51mz _5f0n"]/tbody/tr[2]/td[2]/span/a/div').click()
		self.driver.find_element_by_xpath('//input[@aria-label="你和谁一起?"]').send_keys('xerxes zhang')
		self.driver.find_element_by_xpath('//input[@aria-label="你和谁一起?"]').send_keys(Keys.ENTER)
		time.sleep(1)
		self.driver.find_element_by_xpath('//button[@class="_1mf7 _4jy0 _4jy3 _4jy1 _51sy selected _42ft"]').click()

	def follow(self):
		driver = self.launcher.target_page()
		driver.find_element_by_xpath('//a[@class="_42ft _4jy0 _4jy4 _517h _51sy"]').click()

	def not_follow(self):
		driver = self.launcher.target_page()
		chain = ActionChains(driver)
		implement = driver.find_element_by_xpath('//button[@class="_42ft _4jy0 _3oz- _52-0 _4jy4 _517h _51sy"]')
		chain.move_to_element(implement).perform()
		driver.find_element_by_xpath('//a[@ajaxify="/ajax/follow/unfollow_profile.php?profile_id=100022568024116&location=1"]').click()

# 私信(未关注)
	def send_message(self):
		#发送给未关注的用户
		driver = self.launcher.target_page()
		url = driver.find_element_by_xpath('//a[@class="_51xa _2yfv _3y89"]/a[2]').get_attribute('href')
		driver.get('https://www.facebook.com' + url)
		driver.find_element_by_xpath('//div[@class="_1mf _1mj"]').send_keys('test')
		driver.find_element_by_xpath('//div[@class="_1mf _1mj"]').send_keys(Keys.ENTER)
# 私信(已关注)
	def send_message2(self):
		#发送给已关注的用户
		driver = self.launcher.target_page()
		url = driver.find_element_by_xpath('//a[@class="_51xa _2yfv _3y89"]/a[1]').get_attribute('href')
		driver.get('https://www.facebook.com' + url)
		driver.find_element_by_xpath('//div[@class="_1mf _1mj"]').send_keys('test')
		driver.find_element_by_xpath('//div[@class="_1mf _1mj"]').send_keys(Keys.ENTER)

# 测试id
#id = 'tl_unit_-8182132709408758851'

	def like(self, id):
		driver = self.launcher.target_page()
		driver.find_element_by_xpath('//div[@id="%s"]/div/div[2]/div[2]/form/div[1]/div[1]/div/div[1]/div/div/div/span[1]/div/a'%id).click()


	def comment(self, id):
		driver = self.launcher.target_page()
		driver.find_element_by_xpath('//div[@id="%s"]/div/div[2]/div[2]/form/div[2]/div/div[2]/div/div[2]/div/div/div/div/div'%id).click()
		driver.find_element_by_xpath('//div[@id="%s"]/div/div[2]/div[2]/form/div[2]/div/div[2]/div/div[2]/div/div/div/div/div'%id).send_keys('test comment')
		driver.find_element_by_xpath('//div[@id="%s"]/div/div[2]/div[2]/form/div[2]/div/div[2]/div/div[2]/div/div/div/div/div'%id).send_keys(Keys.ENTER)

# 分享
	def share(self):
		driver = self.launcher.target_page()
		#action_chains = ActionChains(driver)
		driver.find_element_by_xpath('//div[@id="%s"]/div/div[2]/div[2]/form/div[1]/div[1]/div/div[1]/div/div/div/span[3]//span'%id).click()
		time.sleep(3)
		driver.find_element_by_xpath('//div[@id="%s"]/div/div[2]/div[2]/form/div[1]/div[1]/div/div[1]/div/div/div/span[3]/div/span'%id).click()
		#action_chains.key_down(Keys.CONTROL).click(el)
		#driver.find_element_by_xpath('//div[@id="%s"]/div/div[2]/div[2]/form/div[1]/div[1]/div/div[1]/div/div/div/span[3]/div/span'%id).click()
		time.sleep(3)
		driver.find_element_by_xpath('//ul[@class="_54nf"]/li[2]').click()
		time.sleep(8)
		driver.find_element_by_xpath('//div[@class="_1mwp navigationFocus _395  _21mu _5yk1"]').click()
		driver.find_element_by_xpath('//div[@class="notranslate _5rpu"]/div/div/div').send_keys('test')
		time.sleep(3)
		driver.find_element_by_xpath('//button[@data-testid="react_share_dialog_post_button"]').click()


# 好友
	def friends(self,uid):
		uid = '100022568024116'
		self.driver.get('https://www.facebook.com/'+ uid +'/friends')
		self.driver.execute_script("""
			(function () {
			var y = 0;
			var step = 100;
			window.scroll(0, 0);
			function f() {
			if (y < document.body.scrollHeight) {
			y += step;
			window.scroll(0, y);
			setTimeout(f, 150);
			} else {
			window.scroll(0, 0);
			document.title += "scroll-done";
			}
			}
			setTimeout(f, 1500);
			})();
			""")
		time.sleep(3)
		while True:
			if "scroll-done" in driver.title:
				break
			else:
				time.sleep(3)
		for each in driver.find_elements_by_xpath('//div[@id="contentArea"]/div/div[2]/div/div/div/div[2]/div//ul//li'):
			try:
				pic_url = each.find_element_by_xpath('./div/a/img').get_attribute('src')
				name = each.find_element_by_xpath('./div/div/div[2]/div/div[2]/div/a').text
				id = ''.join(re.findall(re.compile('id=(\d+)'),each.find_element_by_xpath('./div/div/div[2]/div/div[2]/div/a').get_attribute('data-hovercard')))
			except Exception as e:
				pass