python2.7 - python模拟登陆微博Element is not currently interactable
大家讲道理
大家讲道理 2017-04-18 10:18:21
0
1
1378

代码来源
我用的py2,改动了点

#encoding=utf-8
import time
from selenium import webdriver
import requests

# 该段代码在ubuntu上能成功运行,并没有在windows上面运行过
# 直接登陆新浪微博
url = 'http://weibo.com/login.php'
driver = webdriver.PhantomJS()
driver.get(url)
print(u'开始登陆')

# 定位到账号密码表单
login_tpye = driver.find_element_by_class_name('info_header').find_element_by_xpath('//a[2]')
login_tpye.click()
time.sleep(3)

name_field = driver.find_element_by_id('loginname')
name_field.clear()
name_field.send_keys('youraccount')

password_field = driver.find_element_by_class_name('password').find_element_by_name('password')
password_field.clear()
password_field.send_keys('yourpassword')

submit = driver.find_element_by_link_text(u'登录')
submit.click()

# 等待页面刷新,完成登陆
time.sleep(5)
print('登陆完成')
sina_cookies = driver.get_cookies()

cookie = [item["name"] + "=" + item["value"] for item in sina_cookies]
cookiestr = '; '.join(item for item in cookie)

# 验证cookie是否有效
redirect_url = 'http://weibo.com/p/1005051921017243/info?mod=pedit_more'
headers = {'cookie': cookiestr}
html = requests.get(redirect_url, headers=headers).text
print(html)

#报错信息如下:

开始登陆
<selenium.webdriver.remote.webelement.WebElement (session="2151def0-f283-11e6-94
63-198aed0167cd", element=":wdc:1487055608184")>
Traceback (most recent call last):
  File "weibo.py", line 20, in <module>
    name_field.clear()
  File "d:\python27\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 87, in clear
    self._execute(Command.CLEAR_ELEMENT)
  File "d:\python27\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 461, in _execute
    return self._parent.execute(command, params)
  File "d:\python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 236, in execute
    self.error_handler.check_response(response)
  File "d:\python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py"
, line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidElementStateException: Message: {"errorMessage
":"Element is not currently interactable and may not be manipulated","request":{
"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection"
:"close","Content-Length":"81","Content-Type":"application/json;charset=UTF-8","
Host":"127.0.0.1:54244","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","m
ethod":"POST","post":"{\"sessionId\": \"2151def0-f283-11e6-9463-198aed0167cd\",
\"id\": \":wdc:1487055608184\"}","url":"/clear","urlParsed":{"anchor":"","query"
:"","file":"clear","directory":"/","path":"/clear","relative":"/clear","port":""
,"host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","s
ource":"/clear","queryKey":{},"chunks":["clear"]},"urlOriginal":"/session/2151de
f0-f283-11e6-9463-198aed0167cd/element/:wdc:1487055608184/clear"}}
Screenshot: available via screen
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

Antworte allen(1)
伊谢尔伦

stackoverflow答案
driver.set_window_size(1920, 1080)

Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage