根据你提供的错误信息,看起来是等待页面元素超时导致的错误。可以尝试增加等待时间或者使用更加精准的元素定位方式。
另外,你的代码中似乎没有将微博账号和密码传递给程序,建议在代码开头将这两个变量赋值。
修改后的代码如下: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 方法时,将这两个参数传入方法中。 |