沙滩星空的博客沙滩星空的博客

python使用selenium登陆淘宝生意参谋

sycm.py代码如下:

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome()

# https://sycm.taobao.com/portal/home.htm https://sycm.taobao.com
sycmLoginUrl = "https://sycm.taobao.com/custom/login.htm?_target=http://sycm.taobao.com/"

driver.get(sycmLoginUrl)
time.sleep(5)
iframe = driver.find_element_by_tag_name("iframe")
driver.switch_to.frame(iframe)
loginNameInput = driver.find_element_by_id("fm-login-id")
loginNameInput.send_keys("username")
passwordInput = driver.find_element_by_id("fm-login-password")
passwordInput.send_keys("pwd123456")
time.sleep(2)
loginBtn = driver.find_element_by_xpath('//*[@id="login-form"]/div[4]/button')

# loginBtn = driver.find_element_by_class_name('fm-button fm-submit password-login')

loginBtn.click()

# 去除广告弹层
# driver.find_element_by_css_selector("span.ebase-ImageTips__dsImageTipsCloseBtn").click()
# driver.find_element_by_xpath('//*[@id="app"]/div/div[1]/div[2]/div[2]/div/span').click()
# driver.find_element_by_css_selector("button.ant-modal-close > span.ant-modal-close-x").click()
# driver.find_element_by_xpath('/html/body/div[4]/div/div[2]/div/div[2]/button/span').click()
print("===这只是一个测试,不必惊慌=== ")
# input("Press <Enter> to Exit")

# driver.quit()


登录后获取网站cookie

diccookie=drive.get_cookies()
fw=open('baiducookie.txt','w')
json.dump(diccookie,fw)
fw.close()

访问不需要登录的页面,读取本地的cookie文件. 再访问需要登录的页面

fr=open('baidu_cookie.txt','r')
cookielist=json.load(fr)
fr.close()
for cookie in cookielist:
    drive.add_cookie(cookie)
         
sleep(5)
drive.get('http://www.baidu.com')
drive.refresh()

用selenium切忌不要在打开网址之前就添加cookie,否则报错InvalidCookieDomainException: Message: invalid cookie domain。

  selenium 一定要先打开网址,然后再加cookie,再打开网址!


selenium报错 Message: invalid cookie domain http://www.python66.com/bbs/165.html
python selenium操作cookie https://www.cnblogs.com/blogzyq/p/11114615.html
Python基础(15)——JSON模块的详述(dumps、loads、dump、load的用法) https://blog.csdn.net/weixin_43625577/article/details/86702654
未经允许不得转载:沙滩星空的博客 » python使用selenium登陆淘宝生意参谋

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址