当硒无头运行时,facebook检测到浏览器自动化

时间:2019-10-16 12:35:26

标签: python facebook selenium webdriver chatbot

带窗口的镶边中为fb chatbot运行此硒脚本时,运行正常在无头模式下,即使使用python虚拟显示,也会被检测到并被阻止

我想使用pyvirtualdisplay在无头模式下运行脚本,同时避免被检测,以便我可以将远程任务作为日常计划任务执行。

这是针对数字海洋中的 ubuntu 18.04液滴

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from pyvirtualdisplay import Display

display = Display(visible=0, size=(800, 800))
display.start()
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--auto-open-devtools-for-tabs")
chrome_options.add_argument("--load-extension=" + me.favoriteExtension)
chrome_options.add_argument("--start-maximized")
chrome_options.add_experimental_option("prefs", { 
    "profile.default_content_setting_values.notifications": 2
})
chrome_options.add_argument('headless')
chrome_options.add_argument('--no-sandbox')
browser = webdriver.Chrome(chrome_options=chrome_options)

def login(submitBtn):
    email = browser.find_element_by_xpath('//*[@id="email"]')
    password = browser.find_element_by_xpath('//*[@id="pass"]')
    btn = browser.find_element_by_xpath(submitBtn)
    email.send_keys(me.fbUser)
    password.send_keys(me.fbPassword)
    btn.click()

def respondToNewMessages():
    browser.get("http://facebook.com/messages/t/")
    print('responding to new msgs')
    conversations = browser.find_elements_by_xpath('*//ul[@aria-label="Conversation List"]/li')
    print('found '+str(len(conversations)) + ' conversations')
    index = 0
    for conversation in conversations:
        index += 1
        firstMsg = conversation.find_element_by_xpath('./div/a/div/div//span/span')
        print('Found first message ' + firstMsg.text + ' conversation #' + str(index))
        msgFontSize = firstMsg.value_of_css_property('font-weight')
        if(not int(msgFontSize) > 400):
            print('conversation #' + str(index) + ' is an old convo')
        else:
            firstMsg = conversation.find_element_by_xpath('./div/a/div/div//span/span')
            firstMsg.click()
            with open('awaymsg.txt') as f:
                for line in f: 
                    ActionChains(browser).send_keys(line).perform() 
                    ActionChains(browser).send_keys(Keys.ENTER).perform()
            f.close()
    return True

browser.get("http://facebook.com") # load the web page
login("//*[@id='u_0_b' and not(@type='hidden')]|//button[@id='loginbutton' and @type='submit' and @name='login' and not(@type='hidden')]") # Attempt login

while ( 'login' in browser.current_url): # if login fails try again and / or dismiss checkpoint
    login("//*[@id='u_0_b' and not(@type='hidden')]|//button[@id='loginbutton' and @type='submit' and @name='login' and not(@type='hidden')]")
    if 'checkpoint' in browser.current_url:
        browser.find_element_by_xpath("//button[@value='Yes']").click()
    browser.get("http://facebook.com/messages/t/") # exit when messages are loaded

browser.implicitly_wait(5) 
conversations = browser.find_elements_by_xpath('*//ul[@aria-label="Conversation List"]/li')  # count conversations

respondToNewMessages()

browser.close()

预期-无头w虚拟显示将反映在窗口镶边中运行。

实际-窗口运行正常,Facebook侦测到无头并阻止了它。

1 个答案:

答案 0 :(得分:0)

pyvirtualdisplay和浏览器的无头模式的功能相同,因此,如果使用pyvirtualdisplay,则不应设置chrome_options.add_argument('headless')

安装xvfb(sudo apt-get install xvfb)并将其设置为后端:

display = Display(backend="xvfb", visible=0, size=(800, 800))