selenium 自动登录出错
本帖最后由 青松100 于 2020-11-23 19:30 编辑小白求助:我想用selenium 登录百度盘的网页,但报错了。求各位大佬帮助。
1. 代码:
2. 报错:
从下面的报错原因,我觉得是在说#11行的ID选择错了,另外没有这个元素。可是我检查了,也不知哪出错了。
我用ctrl + F 输入关键字 iframe ,也没有发现这个网页代码中有iframe的嵌套。
3. 检测工具
4. 以下是代码,因为小甲鱼论坛的主帖如果有一般网址,就会进去审核状态,所以我把百度网盘的地址从代码删掉了。
from selenium import webdriver
from lxml import etree
from time import sleep
#from selenium.webdriver.chrome.options import Options
from selenium.webdriver import ChromeOptions
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
bro = webdriver.Chrome(executable_path='./chromedriver',options=option)
bro.get('')
userName_tag = bro.find_element_by_id('TANGRAM__PSP_4__userName')
password_tag = bro.find_element_by_id('TANGRAM__PSP_4__password')
userName_tag.send_keys('abcd@163.com')
sleep(1)
password_tag.send_keys('12345678')
sleep(1)
5. 报错的文字版
(base) F:\python\3>C:/ProgramData/Anaconda3/python.exe f:/python/3/pandownload.py
DevTools listening on ws://127.0.0.1:54226/devtools/browser/bc4df8a8-b74a-4585-b654-4ab70aafb6db
Traceback (most recent call last):
File "f:/python/3/pandownload.py", line 11, in <module>
userName_tag = bro.find_element_by_id('TANGRAM__PSP_4__userName')
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":""}
(Session info: chrome=87.0.4280.66) 本帖最后由 suchocolate 于 2020-11-23 20:08 编辑
from selenium import webdriver
from time import sleep
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as expected
from selenium.webdriver.support.wait import WebDriverWait
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
bro = webdriver.Chrome(executable_path='./chromedriver',options=option)
bro.get('xxxxx')
wait = WebDriverWait(bro, 10) # 浏览器显示等待
wait.until(expected.visibility_of_element_located((By.ID, 'TANGRAM__PSP_4__userName'))) # 当用户名可见,再继续
userName_tag = bro.find_element_by_id('TANGRAM__PSP_4__userName')
password_tag = bro.find_element_by_id('TANGRAM__PSP_4__password')
userName_tag.send_keys('abcd@163.com')
sleep(1)
password_tag.send_keys('12345678')
sleep(1)
另外,selenium不带cookie会提示手工滑动验证,这个有难度。如果从浏览器手工导入cookie,不如用requests导入cookie,操作更快,不推荐用selenium。 suchocolate 发表于 2020-11-23 20:05
另外,selenium不带cookie会提示手工滑动验证,这个有难度。如果从浏览器手工导入cookie,不如用requests ...
非常谢谢巧克力大神费心看我这么冗长的帖子,帮助我解决这个问题。我就先不搞这个自动登陆百度盘了,等我先把我正在学习的这套视频学完之后,练熟之后,再多看几套视频再说。
suchocolate 发表于 2020-11-23 20:05
另外,selenium不带cookie会提示手工滑动验证,这个有难度。如果从浏览器手工导入cookie,不如用requests ...
我懂了。原来是 死了鸟 操作太快了,把浏览器都 吓死辣。
你教我的是显式等待。
再次谢谢巧克力老师。
页:
[1]