Python 3.x Selenium-登录Facebook会导致Web驱动程序无限期暂停

Python 3.x Selenium-登录Facebook会导致Web驱动程序无限期暂停,python-3.x,selenium,selenium-webdriver,selenium-chromedriver,Python 3.x,Selenium,Selenium Webdriver,Selenium Chromedriver,我有一个简单的程序,可以登录Facebook并获得3个URL: def setup_driver(): prefs = {"profile.default_content_setting_values.notifications": 2} chrome_options = webdriver.ChromeOptions() chrome_options.add_experimental_option("prefs", prefs) chrome_options.a

我有一个简单的程序,可以登录Facebook并获得3个URL:

def setup_driver():
    prefs = {"profile.default_content_setting_values.notifications": 2}
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_experimental_option("prefs", prefs)
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--no-sandbox')
    driver = webdriver.Chrome(executable_path="./chromedriver_linux",
                              chrome_options=chrome_options)
    return driver

def log_into_facebook(driver):
    driver.get("https://www.facebook.com/")
    email_field = driver.find_element_by_id("email")
    email_field.send_keys("<MY EMAIL ADDRESS>")
    password_field = driver.find_element_by_id("pass")
    password_field.send_keys("<MY FB PASSWORD>")
    driver.find_element_by_id("loginbutton").click()

if __name__ == "__main__":

    driver = setup_driver()
    log_into_facebook(driver)
    print("before getting url 1")
    driver.get('https://facebook.com/2172111592857876')
    print("before getting url 2")


 #Stackoverflow is breaking indentation
driver.get('https://www.facebook.com/beaverconfessions/posts/2265225733546461')
    print("before getting url 3")
    driver.get('https://www.facebook.com/beaverconfessions/posts/640487179353666')
    print("finished getting 3 urls")
def设置_驱动程序():
prefs={“profile.default\u content\u setting\u value.notifications”:2}
chrome\u options=webdriver.ChromeOptions()
chrome_选项。添加_实验_选项(“prefs”,prefs)
chrome_选项。添加_参数('--headless')
chrome_选项。添加_参数('--no sandbox')
driver=webdriver.Chrome(executable_path=“./chromedriver_linux”,
chrome\u选项=chrome\u选项)
返回驱动器
def登录到facebook(驱动程序):
驱动程序。获取(“https://www.facebook.com/")
email\u field=驱动程序。通过\u id(“email”)查找\u元素
电子邮件字段。发送密钥(“”)
password\u field=driver.find\u element\u by\u id(“pass”)
密码\字段。发送\密钥(“”)
驱动程序。按id(“登录按钮”)查找元素。单击()
如果名称=“\uuuuu main\uuuuuuuu”:
驱动程序=设置\驱动程序()
登录facebook(驱动程序)
打印(“在获取url 1之前”)
司机,上车https://facebook.com/2172111592857876')
打印(“在获取url 2之前”)
#Stackoverflow正在破坏缩进
司机,上车https://www.facebook.com/beaverconfessions/posts/2265225733546461')
打印(“在获取url 3之前”)
司机,上车https://www.facebook.com/beaverconfessions/posts/640487179353666')
打印(“完成获取3个URL”)
在我的本地机器上,这个程序运行良好。但是,在我的AWS EC2实例上,这个程序使我的实例不可用(Python脚本将在“获取url 2之前”之后挂起/暂停)打印到控制台。当脚本挂起时,EC2实例将变得非常慢,以至于实例上的其他程序也将停止正常工作。我需要使用Ctrl-C强制关闭程序,以便实例再次开始响应。)。但是,如果我注释掉
登录到facebook(驱动程序)
,那么程序运行正常

我会尝试获取stacktrace,但程序实际上并没有崩溃,它只是永远不会到达“获取url 3之前”

这毫无价值,以前我用一个类似的程序得到了“无效会话id”错误(它也登录到Facebook,然后多次调用
driver.get

更新:从
webdriver
中删除
--no sandbox
选项似乎可以解决问题。我不知道为什么。我最初有这个选项,因为我以前有一个“无法修复打开的页面”错误,我读到--“没有沙盒”会修复这个错误

    chrome_options.add_argument('--no-sandbox')
Roymunson报告说,解决悬挂问题的适当方法是:


避免在webdriver中指定
--no sandbox
选项。

崩溃时是否存在特定于浏览器的错误?为什么不使用API呢?崩溃时有错误吗?你说的使我的实例不可用、登录Facebook崩溃Selenium/Chrome驱动程序和无效会话到底是什么意思?你能用相关的跟踪/错误日志更新问题吗?@DebanjanB我更新了我的问题,使之更具体。如果不是错误堆栈跟踪,跟踪级别日志可能也会有帮助