解决cookie设置失败问题

This commit is contained in:
白茶清欢 2023-01-24 18:50:43 +08:00
parent 4c99d24de6
commit 6446cd58f1
2 changed files with 218 additions and 12 deletions

View File

@ -60,7 +60,7 @@ type Chrome struct {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:14 2023/1/24
func (c *Chrome) Render() {
func (c *Chrome) Render(cookieList []*selenium.Cookie) {
//链接本地的浏览器 chrome
caps := selenium.Capabilities{
//"browserName": "/Applications/Google Chrome Dev.app/Contents/MacOS/Google Chrome Dev",
@ -83,23 +83,24 @@ func (c *Chrome) Render() {
}
//以上是设置浏览器参数
caps.AddChrome(chromeCaps)
url := "https://detail.tmall.hk/hk/item.htm?id=605481322232&rn=bbb58fe2f3e13e03b21df9c396f23fbc&abbucket=12"
w_b1, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", c.port))
if err != nil {
fmt.Println("connect to the webDriver faild", err.Error())
return
}
w_b1.AddCookie(&selenium.Cookie{
Name: "",
Value: "",
Path: "",
Domain: "",
Secure: false,
Expiry: 0,
})
_ = w_b1.Get(url)
for _, itemCookie := range cookieList {
err = w_b1.AddCookie(itemCookie)
fmt.Println(err)
}
allCookie, _ := w_b1.GetCookies()
fmt.Println(allCookie)
err = w_b1.Get(url)
imgByteData, _ := w_b1.Screenshot()
ioutil.WriteFile("test.png", imgByteData, 0777)
_ = ioutil.WriteFile("test.png", imgByteData, 0777)
//获取网页源码
pageSource, err := w_b1.PageSource()

View File

@ -7,9 +7,214 @@
// Date : 2023-01-24 16:16
package util
import "testing"
import (
"encoding/json"
"testing"
"github.com/tebeka/selenium"
)
type itemCookie struct {
Domain string `json:"domain"`
ExpirationDate float64 `json:"expirationDate"`
HostOnly bool `json:"hostOnly"`
HTTPOnly bool `json:"httpOnly"`
Name string `json:"name"`
Path string `json:"path"`
SameSite string `json:"sameSite"`
Secure bool `json:"secure"`
Session bool `json:"session"`
StoreID interface{} `json:"storeId"`
Value string `json:"value"`
}
func TestNewChrome(t *testing.T) {
instance, _ := NewChrome("/Users/zhangdeman/Downloads/chromedriver", 9515, true, nil)
instance.Render()
cookieJson := `[
{
"domain": ".tmall.hk",
"expirationDate": 1690105309,
"hostOnly": false,
"httpOnly": false,
"name": "isg",
"path": "/",
"sameSite": "no_restriction",
"secure": true,
"session": false,
"storeId": null,
"value": "BIaGaoCcGV5rRs1MLXd6yKZh13oI58qhGIcoyHCvZamEcyaN2HVhsLYBS6-_W8K5"
},
{
"domain": ".tmall.hk",
"expirationDate": 1690105309,
"hostOnly": false,
"httpOnly": false,
"name": "tfstk",
"path": "/",
"sameSite": null,
"secure": false,
"session": false,
"storeId": null,
"value": "cg9FBFXpnTY1tO--kOXy_d93nw_dZzABN5Sct4lddHYKLG5hiAzRjaHP7aqar6f.."
},
{
"domain": ".tmall.hk",
"hostOnly": false,
"httpOnly": true,
"name": "cookie2",
"path": "/",
"sameSite": "no_restriction",
"secure": true,
"session": true,
"storeId": null,
"value": "11a2e3f5c45325625d3078fb518e12c2"
},
{
"domain": ".tmall.hk",
"expirationDate": 1989017049.363698,
"hostOnly": false,
"httpOnly": true,
"name": "enc",
"path": "/",
"sameSite": "no_restriction",
"secure": true,
"session": false,
"storeId": null,
"value": "6CoIBy8lnAje2hQ5K99yGofrAdPkNuvRIKCK5JA5Q1%2Boub3YNh5iNsNvpfU4Gc%2F9X98U3fXBxBLv3PYp6%2Fw9Eg%3D%3D"
},
{
"domain": ".tmall.hk",
"expirationDate": 2303824744,
"hostOnly": false,
"httpOnly": false,
"name": "cna",
"path": "/",
"sameSite": "no_restriction",
"secure": true,
"session": false,
"storeId": null,
"value": "Unx0GIPP62UCAdIMnVT6ZNWT"
},
{
"domain": ".tmall.hk",
"hostOnly": false,
"httpOnly": false,
"name": "_tb_token_",
"path": "/",
"sameSite": "no_restriction",
"secure": true,
"session": true,
"storeId": null,
"value": "5777de41b7551"
},
{
"domain": ".tmall.hk",
"expirationDate": 1690105309,
"hostOnly": false,
"httpOnly": false,
"name": "l",
"path": "/",
"sameSite": null,
"secure": false,
"session": false,
"storeId": null,
"value": "fB_jjhQqT3bcamXsBOfwFurza77tQIRAguPzaNbMi9fPO45e5NgCW6R3X-LwCnGVFs9vR38_FFteBeYBcImK10ew8c9WeIkmnmOk-Wf.."
},
{
"domain": ".tmall.hk",
"expirationDate": 1705193049.363653,
"hostOnly": false,
"httpOnly": false,
"name": "lid",
"path": "/",
"sameSite": "no_restriction",
"secure": true,
"session": false,
"storeId": null,
"value": "baichaqinghuan"
},
{
"domain": "citizenwatchhk.tmall.hk",
"expirationDate": 1677145308,
"hostOnly": true,
"httpOnly": false,
"name": "pnm_cku822",
"path": "/",
"sameSite": null,
"secure": false,
"session": false,
"storeId": null,
"value": ""
},
{
"domain": ".tmall.hk",
"hostOnly": false,
"httpOnly": true,
"name": "sgcookie",
"path": "/",
"sameSite": "no_restriction",
"secure": true,
"session": true,
"storeId": null,
"value": "E100IilftBf8v72hUSyVYv1LxeVnT75WmX5JvCC%2FiwaVX6Jw%2F0VN5tvc5jeI1pmLlV6PQztlnaJAV7s%2BgN7OZop9YcNrm7SwWEzf%2FVxygdIEDEOMnWyqApcrGHr8YEjkciwn"
},
{
"domain": ".tmall.hk",
"hostOnly": false,
"httpOnly": false,
"name": "t",
"path": "/",
"sameSite": "no_restriction",
"secure": true,
"session": true,
"storeId": null,
"value": "6f803235fb4e32d8ef21df8238179fbe"
},
{
"domain": ".tmall.hk",
"hostOnly": false,
"httpOnly": false,
"name": "tracknick",
"path": "/",
"sameSite": "no_restriction",
"secure": true,
"session": true,
"storeId": null,
"value": "baichaqinghuan"
},
{
"domain": ".tmall.hk",
"expirationDate": 1674559183,
"hostOnly": false,
"httpOnly": false,
"name": "xlly_s",
"path": "/",
"sameSite": "no_restriction",
"secure": true,
"session": false,
"storeId": null,
"value": "1"
}
]`
var inputCookieList []itemCookie
_ = json.Unmarshal([]byte(cookieJson), &inputCookieList)
cookieList := make([]*selenium.Cookie, 0)
for _, item := range inputCookieList {
expiry := uint(item.ExpirationDate)
if expiry == 0 {
expiry = 1690105309
}
if item.Domain == ".tmall.hk" {
item.Domain = "citizenwatchhk" + item.Domain
}
cookieList = append(cookieList, &selenium.Cookie{
Name: item.Name,
Value: item.Value,
Path: item.Path,
Domain: item.Domain,
Secure: item.Secure,
Expiry: expiry,
})
}
instance, _ := NewChrome("/Users/zhangdeman/Downloads/chromedriver", 9515, true, nil)
instance.Render(cookieList)
}