使用selenium模拟登陆淘宝报错
from selenium import webdriverfrom selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class tb_infos:
# 初始化对象
def __init__(self):
url = 'https://login.taobao.com/member/login.jhtml'
self.url = url
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {'profile.managed_default_content_settings.images': 2})
options.add_experimental_option('excludeSwitches', ['enable-automation'])
self.browser = webdriver.Chrome(options=options)
self.wait = WebDriverWait(self.browser, 10)
# 登录淘宝
def login(self):
self.browser.get(self.url)
password_login = self.wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.qrcode-login > .login-links > .forget-pws')))
password_login.click()
wb_login = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.weibo-login')))
wb_login.click()
wb_user = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.username > .W_input')))
wb_user.send_keys(wb_username)
wb_pwd = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.password > .W_input')))
wb_pwd.send_keys(wb_password)
submit = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.btn-tip > a > span')))
submit.click()
tb_name = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,
'.site-nav-bd > .site-nav-bd-1 > li#J_SiteNavLogin > .site-nav-menu-hd > .site-nav-user > a.site-nav-login-info-nick')))
print(tb_name.text)
if __name__ == '__main__':
wb_username = '微博账号'
wb_password = '微博密码'
a = tb_infos()
a.login()
运行的时候报错:
Traceback (most recent call last):
File "D:/项目/Python/淘宝登录/tb_login.py", line 53, in <module>
a.login()
File "D:/项目/Python/淘宝登录/tb_login.py", line 28, in login
EC.presence_of_element_located((By.CSS_SELECTOR, '.qrcode-login > .login-links > .forget-pws')))
File "D:\Program Files\Anaconda\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
进程已结束,退出代码 1
EC.presence_of_element_located((By.CSS_SELECTOR, '.qrcode-login > .login-links > .forget-pws')))
网页上定位不到这个CSS_SELECTOR gink 发表于 2019-12-20 14:13
**** 作者被禁止或删除 内容自动屏蔽 ****
是timeout exception,是超时了
应该“time.sleep”一会儿让它加载出来 根据你提供的错误信息,看起来是等待页面元素超时导致的错误。可以尝试增加等待时间或者使用更加精准的元素定位方式。
另外,你的代码中似乎没有将微博账号和密码传递给程序,建议在代码开头将这两个变量赋值。
修改后的代码如下:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class tb_infos:
# 初始化对象
def __init__(self):
url = 'https://login.taobao.com/member/login.jhtml'
self.url = url
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {'profile.managed_default_content_settings.images': 2})
options.add_experimental_option('excludeSwitches', ['enable-automation'])
self.browser = webdriver.Chrome(options=options)
self.wait = WebDriverWait(self.browser, 20)
# 登录淘宝
def login(self, wb_username, wb_password):
self.browser.get(self.url)
password_login = self.wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.qrcode-login > .login-links > .forget-pws')))
password_login.click()
wb_login = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.weibo-login')))
wb_login.click()
wb_user = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.username > .W_input')))
wb_user.send_keys(wb_username)
wb_pwd = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.password > .W_input')))
wb_pwd.send_keys(wb_password)
submit = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.btn-tip > a > span')))
submit.click()
tb_name = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,
'.site-nav-bd > .site-nav-bd-1 > li#J_SiteNavLogin > .site-nav-menu-hd > .site-nav-user > a.site-nav-login-info-nick')))
print(tb_name.text)
if __name__ == '__main__':
wb_username = '微博账号'
wb_password = '微博密码'
a = tb_infos()
a.login(wb_username, wb_password)
其中,我增加了一个 login 方法的两个参数 wb_username 和 wb_password,并且在最后一行调用 login 方法时,将这两个参数传入方法中。
页:
[1]