Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python facebook在selenium运行headless时检测到浏览器自动化_Python_Facebook_Selenium_Webdriver_Chatbot - Fatal编程技术网

Python facebook在selenium运行headless时检测到浏览器自动化

Python facebook在selenium运行headless时检测到浏览器自动化,python,facebook,selenium,webdriver,chatbot,Python,Facebook,Selenium,Webdriver,Chatbot,在窗口chrome中运行fb chatbot的selenium脚本时运行良好在无头模式下,即使使用python虚拟显示,也会检测到并阻止它 我想使用pyvirtualdisplay在无头模式下运行脚本,同时避免检测,这样我就可以作为日常计划任务远程执行 这是为数字海洋上的Ubuntu18.04水滴而设计的 从selenium导入webdriver 从selenium.webdriver.common.keys导入密钥 从selenium.webdriver.support.ui导入WebDriv

窗口chrome中运行fb chatbot的selenium脚本时运行良好在无头模式下,即使使用python虚拟显示,也会检测到并阻止它

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

这是为数字海洋上的Ubuntu18.04水滴而设计的

从selenium导入webdriver
从selenium.webdriver.common.keys导入密钥
从selenium.webdriver.support.ui导入WebDriverWait
从selenium.webdriver.common.action\u链导入ActionChains
从selenium.webdriver.support将预期的_条件导入为EC
从selenium.webdriver.common.by导入
从selenium.webdriver.support将预期的_条件导入为EC,从2.26.0开始提供
从pyvirtualdisplay导入显示
显示=显示(可见=0,大小=(800800))
display.start()
chrome\u options=webdriver.ChromeOptions()
chrome_选项。添加_参数(“--自动打开选项卡的devtools”)
chrome\u选项。添加参数(“--load extension=“+me.favoriteExtension”)
chrome\u选项。添加\u参数(“--start maximized”)
chrome_选项。添加_实验_选项(“prefs”,{
“配置文件。默认内容设置值。通知”:2
})
chrome\u选项。添加\u参数('headless')
chrome_选项。添加_参数('--no sandbox')
浏览器=webdriver.Chrome(Chrome\u选项=Chrome\u选项)
def登录(submitBtn):
email=browser。通过xpath('/*[@id=“email”]”查找元素
password=browser。通过xpath('/*[@id=“pass”]”查找元素
btn=浏览器。通过xpath查找元素(submitBtn)
电子邮件。发送密钥(me.fbUser)
密码。发送密钥(me.fbPassword)
点击()
def respondToNewMessages():
browser.get(“http://facebook.com/messages/t/")
打印('响应新MSG')
conversations=browser。通过xpath('*/ul[@aria label=“Conversation List”]/li'查找元素
打印('found'+str(len(对话))+'conversations')
索引=0
对于对话中的对话:
指数+=1
firstMsg=conversation.通过xpath('./div/a/div/div//span/span')查找元素
打印('Found first message'+firstMsg.text+'conversation#'+str(索引))
msgFontSize=firstMsg.value\u属性('font-weight')的值
如果(非int(msgFontSize)>400):
print('conversation#'+str(index)+'是一个旧的conva')
其他:
firstMsg=conversation.通过xpath('./div/a/div/div//span/span')查找元素
firstMsg.click()
将open('awaymsg.txt')作为f:
对于f中的行:
ActionChains(浏览器)。发送_键(行)。执行()
ActionChains(浏览器).send_key(key.ENTER).perform()
f、 关闭()
返回真值
browser.get(“http://facebook.com)加载网页
登录(“//*[@id='u_0_b'和not(@type='hidden')].//按钮[@id='loginbutton'和@type='submit'和@name='login'和not(@type='hidden')])尝试登录
while('login'在browser.current_url中):#如果登录失败,请重试和/或取消检查点
登录(“//*[@id='u_0_b'和not(@type='hidden')].//按钮[@id='loginbutton'和@type='submit'和@name='login'和not(@type='hidden')]))
如果browser.current\u url中有“checkpoint”:
浏览器。通过xpath(//按钮[@value='Yes'])查找元素。单击()
browser.get(“http://facebook.com/messages/t/“”#加载消息时退出
浏览器。隐式等待(5)
conversations=browser。通过xpath('*/ul[@aria label=“Conversation List”]/li')查找元素
respondToNewMessages()
browser.close()
预期-无头w虚拟显示屏将反映在窗口镀铬中运行


实际-窗口运行正常,无头被Facebook检测和阻止。

pyvirtualdisplay
和浏览器的无头模式也是这样做的,因此如果使用
pyvirtualdisplay
,则不应设置
chrome\u选项。添加参数('headless')

安装xvfb(
sudo-apt-get-Install-xvfb
),并使用以下命令将其设置为后端:

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

那么,你的问题是什么?如何规避Facebook的安全措施?