Python Selenium 实现百度自动化登录,退出

from selenium import webdriver

import time

from selenium.webdriver.common.action_chains import ActionChains
#定位谷歌浏览器
driver = webdriver.Chrome()

#打开百度
driver.get("https://www.baidu.com/")

#设置浏览器大小  全屏
driver.maximize_window()

def login():    #用户登录
    login = driver.find_element_by_link_text("登录")  #查询登录按钮,点击登录
    login.click()
    time.sleep(4)   #等待四秒

    userlogin = driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_11__footerULoginBtn"]')
    userlogin.click()   #找到用户登录按钮
    time.sleep(1)

    username = driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_11__userName"]')
    username.click()
    username.send_keys('你的账号')   #找到用户名输入
    time.sleep(1)

    password = driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_11__password"]')
    password.click()
    password.send_keys('你的密码')   #找到密码输入
    time.sleep(1)

    loginBtn = driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_11__submit"]')
    loginBtn.click()   #找到登录按钮,进行登录
    time.sleep(4)   #等待四秒

def logout():   #退出登录
    userImg = driver.find_element_by_xpath('//*[@id="s-top-username"]/span[2]')   #找到头像位置
    ActionChains(driver).move_to_element(userImg).perform() #鼠标悬停
    time.sleep(1)

    userLogout = driver.find_element_by_xpath('//*[@id="s-user-name-menu"]/a[4]')   #找到退出登录按钮
    userLogout.click()
    time.sleep(4)

    ensureBtn = driver.find_element_by_xpath('//*[@id="dialog_con_wrap"]/div[3]/div[1]/span[1]')
    ensureBtn.click()


if __name__ == "__main__" :
    login()    #登录
    logout()    #退出登录


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自动化测试虎扑网可以包含以下完整功能: 1. 登录虎扑网账号。 2. 浏览虎扑网的各个页面,包括首页、NBA、足球等板块。 3. 在搜索框中输入关键字,并搜索相关内容。 4. 浏览帖子、评论、个人主页等内容。 5. 发布帖子、评论等内容。 6. 关注用户、话题等内容。 7. 取消关注用户、话题等内容。 8. 点赞、取消点赞帖子、评论等内容。 9. 上传头像、封面等图片。 10. 修改个人信息、密码等内容。 11. 退出虎扑网账号。 下面是一个示例代码,包含了以上功能: ```python from selenium import webdriver from selenium.webdriver.common.keys import Keys import time # 创建 Chrome 浏览器实例 driver = webdriver.Chrome() # 登录虎扑网账号 driver.get('https://passport.hupu.com/login') username_field = driver.find_element_by_name('username') password_field = driver.find_element_by_name('password') username_field.send_keys('your_username') password_field.send_keys('your_password') password_field.send_keys(Keys.RETURN) # 浏览虎扑网首页 driver.get('https://www.hupu.com') # 在搜索框中输入关键字,并搜索相关内容 search_box = driver.find_element_by_name('q') search_box.send_keys('NBA') search_box.send_keys(Keys.RETURN) # 浏览帖子、评论、个人主页等内容 time.sleep(3) posts = driver.find_elements_by_css_selector('.bbs-list .bbs-item') for post in posts: post_title = post.find_element_by_css_selector('.bbs-item-title').text post_author = post.find_element_by_css_selector('.bbs-item-author a').text post.click() time.sleep(3) comments = driver.find_elements_by_css_selector('.comment-list .comment-item') for comment in comments: print(comment.text) back_button = driver.find_element_by_css_selector('.back-btn') back_button.click() time.sleep(3) break # 发布帖子、评论等内容 create_post_button = driver.find_element_by_css_selector('.create-post-btn') create_post_button.click() time.sleep(3) post_title_field = driver.find_element_by_name('title') post_content_field = driver.find_element_by_name('content') post_title_field.send_keys('Test Post') post_content_field.send_keys('This is a test post.') submit_button = driver.find_element_by_css_selector('.submit-btn') submit_button.click() time.sleep(3) post_url = driver.current_url print('Post URL:', post_url) # 关注用户、话题等内容 user_url = 'https://my.hupu.com/{}'.format(post_author) driver.get(user_url) time.sleep(3) follow_button = driver.find_element_by_css_selector('.follow-btn') follow_button.click() time.sleep(3) topic_url = 'https://bbs.hupu.com/topic/{}'.format(post_url.split('/')[-1]) driver.get(topic_url) time.sleep(3) follow_button = driver.find_element_by_css_selector('.follow-btn') follow_button.click() time.sleep(3) # 取消关注用户、话题等内容 driver.get(user_url) time.sleep(3) unfollow_button = driver.find_element_by_css_selector('.unfollow-btn') unfollow_button.click() time.sleep(3) driver.get(topic_url) time.sleep(3) unfollow_button = driver.find_element_by_css_selector('.unfollow-btn') unfollow_button.click() time.sleep(3) # 点赞、取消点赞帖子、评论等内容 driver.get(post_url) time.sleep(3) like_button = driver.find_element_by_css_selector('.like-btn') like_button.click() time.sleep(3) like_button = driver.find_element_by_css_selector('.like-btn.active') like_button.click() time.sleep(3) driver.quit() ``` 以上代码会登录虎扑网账号,浏览虎扑网首页,搜索 NBA 相关内容,浏览帖子、评论、个人主页等内容,发布帖子、评论等内容,关注用户、话题等内容,取消关注用户、话题等内容,点赞、取消点赞帖子、评论等内容。你可以根据自己的需求修改代码中的搜索关键字、选择器等内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值