Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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的selenium请求在网页上发布帖子以登录站点_Python_Selenium_Selenium Webdriver_Python Requests_Selenium Chromedriver - Fatal编程技术网

如何使用带有Python的selenium请求在网页上发布帖子以登录站点

如何使用带有Python的selenium请求在网页上发布帖子以登录站点,python,selenium,selenium-webdriver,python-requests,selenium-chromedriver,Python,Selenium,Selenium Webdriver,Python Requests,Selenium Chromedriver,我必须登录一个网站(例如,我将使用facebook.com)。我可以使用selenium管理登录过程,但我需要使用POST。我尝试使用请求,但无法将所需信息传递给selenium webdriver,以便以登录用户的身份进入站点。我在网上发现了一个集成了selenium和requests的库,但问题是没有文档,我也被困在同一个故事中 与硒的要求 webdriver = Chrome() url = "https://www.facebook.com" webdriver.get(url) pa

我必须登录一个网站(例如,我将使用facebook.com)。我可以使用selenium管理登录过程,但我需要使用POST。我尝试使用请求,但无法将所需信息传递给selenium webdriver,以便以登录用户的身份进入站点。我在网上发现了一个集成了selenium和requests的库,但问题是没有文档,我也被困在同一个故事中

与硒的要求

webdriver = Chrome()
url = "https://www.facebook.com"
webdriver.get(url)

params = {
    'email': 'my_email',
    'pass': 'my_password'
    }
resp = webdriver.request('POST','https://www.facebook.com/login/device-based/regular/login/?login_attempt=1&lwv=110', params) 

webdriver.get(url) 
# I hoped that the new page open was the one with me logged in but it did not works

使用Selenium和传递cookie的请求

driver = webdriver.Chrome()
webdriver = Chrome()
url = "https://www.facebook.com"
driver.get(url)

#storing the cookies generated by the browser
request_cookies_browser = driver.get_cookies()

#making a persistent connection using the requests library
params = {
    'email': 'my_email',
    'pass': 'my_password'
    }

s = requests.Session()
#passing the cookies generated from the browser to the session
c = [s.cookies.set(c['name'], c['value']) for c in request_cookies_browser]
resp = s.post('https://www.facebook.com/login/device-based/regular/login/?login_attempt=1&lwv=110', params) #I get a 200 status_code
#passing the cookie of the response to the browser
dict_resp_cookies = resp.cookies.get_dict()
response_cookies_browser = [{'name':name, 'value':value} for name, value in dict_resp_cookies.items()]
c = [driver.add_cookie(c) for c in response_cookies_browser]
driver.get(url)

在这两种情况下,如果最后我打印cookies,看起来从一开始就有所改变,但页面仍然是带有登录表单的页面

这是我尝试过的代码,我把两种尝试都放在这里了,但是找到这两种方法中的一种就足够了。 有人可以帮助我,知道我必须做什么,或者在我登录时更改以打开页面

提前谢谢你

我也有同样的问题。 在代码中,只需按原样传递参数。 在代码中,数据=params将是
In:

resp = webdriver.request('POST','https://www.facebook.com/login/device-based/regular/login/?login_attempt=1&lwv=110', params)
我也有同样的问题。 在代码中,只需按原样传递参数。 在代码中,数据=params将是
In:

resp = webdriver.request('POST','https://www.facebook.com/login/device-based/regular/login/?login_attempt=1&lwv=110', params)

老实说,我甚至不明白你为什么要尝试使用Selenium如果你只想在网络流量级别上进行交互,你也可以直接使用请求库,完全不使用Selenium。你不以正常方式与网页进行交互有什么原因吗?也许我可以按照你的建议只使用请求,我已经开始使用selenium,并且我已经创建了一个函数,它是一个爬虫,可以在页面中搜索一些信息(我在登录后到达的页面)。假设只使用请求,你能解释我该怎么做吗?我的意思是,我写了这篇文章,但我不明白如何在登录后拥有页面(HTML就足够了),因为你没有真正使用Selenium。您正在使用一个库,它包装请求并假装与selenium相关,但实际上并不相关。在从POST请求获得一个respose之后,您只需要查看响应内容即可获得HTML()。我也不清楚你到底想做什么。如果你想与网页进行交互,Selenium是非常合适的,你的问题是暗示你不需要。老实说,我甚至不明白你为什么要尝试使用Selenium如果你只想在网络流量级别进行交互,你也可以直接使用请求库,完全不使用Selenium。你不以正常方式与网页进行交互有什么原因吗?也许我可以按照你的建议只使用请求,我已经开始使用selenium,并且我已经创建了一个函数,它是一个爬虫,可以在页面中搜索一些信息(我在登录后到达的页面)。假设只使用请求,你能解释我该怎么做吗?我的意思是,我写了这篇文章,但我不明白如何在登录后拥有页面(HTML就足够了),因为你没有真正使用Selenium。您正在使用一个库,它包装请求并假装与selenium相关,但实际上并不相关。在从POST请求获得一个respose之后,您只需要查看响应内容即可获得HTML()。我也不清楚你到底想做什么。如果您想与网页交互,Selenium是非常合适的,您的问题暗示您不想与网页交互。