diff --git a/.gitignore b/.gitignore index df8e306..64aa8fc 100644 --- a/.gitignore +++ b/.gitignore @@ -19,4 +19,5 @@ .idea .vscode mail_test.go +test.png diff --git a/chrome.go b/chrome.go index cb27a55..22b4f28 100644 --- a/chrome.go +++ b/chrome.go @@ -9,10 +9,9 @@ package util import ( "fmt" - "io/ioutil" - "github.com/tebeka/selenium" "github.com/tebeka/selenium/chrome" + "os" ) // NewChrome ... @@ -84,13 +83,14 @@ func (c *Chrome) Render(cookieList []*selenium.Cookie) { //以上是设置浏览器参数 caps.AddChrome(chromeCaps) - url := "https://detail.tmall.hk/hk/item.htm?id=605481322232&rn=bbb58fe2f3e13e03b21df9c396f23fbc&abbucket=12" + url := "https://zhangdeman.cn/archives/bbd86236.html" 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()) + fmt.Println("connect to the webDriver failed", err.Error()) return } _ = w_b1.Get(url) + fmt.Println(w_b1.DoubleClick()) for _, itemCookie := range cookieList { err = w_b1.AddCookie(itemCookie) @@ -102,12 +102,12 @@ func (c *Chrome) Render(cookieList []*selenium.Cookie) { fmt.Println(allCookie) err = w_b1.Get(url) imgByteData, _ := w_b1.Screenshot() - _ = ioutil.WriteFile("test.png", imgByteData, 0777) + _ = os.WriteFile("test.png", imgByteData, 0777) //获取网页源码 pageSource, err := w_b1.PageSource() if err != nil { - fmt.Println("get page faild", err.Error()) + fmt.Println("get page failed", err.Error()) return } fmt.Print(pageSource) diff --git a/chrome_test.go b/chrome_test.go index 09b6fbf..0e94b2a 100644 --- a/chrome_test.go +++ b/chrome_test.go @@ -8,345 +8,14 @@ package util 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) { - cookieJson := `[ - { - "domain":".tmall.hk", - "hostOnly":false, - "httpOnly":false, - "name":"csg", - "path":"/", - "sameSite":"no_restriction", - "secure":true, - "session":true, - "storeId":null, - "value":"fc697e5b" - }, - { - "domain":".tmall.hk", - "hostOnly":false, - "httpOnly":false, - "name":"sg", - "path":"/", - "sameSite":"no_restriction", - "secure":true, - "session":true, - "storeId":null, - "value":"686" - }, - { - "domain":".tmall.hk", - "hostOnly":false, - "httpOnly":true, - "name":"cookie17", - "path":"/", - "sameSite":"no_restriction", - "secure":true, - "session":true, - "storeId":null, - "value":"UoYenbvzgsH2Rg%3D%3D" - }, - { - "domain":".tmall.hk", - "expirationDate":1691071546, - "hostOnly":false, - "httpOnly":false, - "name":"isg", - "path":"/", - "sameSite":"no_restriction", - "secure":true, - "session":false, - "storeId":null, - "value":"BCUlEDTRqvr1_M7BGtKJXclsNOdfYtn0p7prhScK4dxrPkWw77LpxLPcyKJIJfGs" - }, - { - "domain":".tmall.hk", - "expirationDate":1691071546, - "hostOnly":false, - "httpOnly":false, - "name":"tfstk", - "path":"/", - "sameSite":null, - "secure":false, - "session":false, - "storeId":null, - "value":"cxL1B7jgeKLEdyg0SOGEg-9RLpQAwNrlGV661AqpnM-PVT1cBzW7X890LZ5AO" - }, - { - "domain":".tmall.hk", - "hostOnly":false, - "httpOnly":true, - "name":"cookie2", - "path":"/", - "sameSite":"no_restriction", - "secure":true, - "session":true, - "storeId":null, - "value":"18a65977fe14410e7165b578f17cfcc9" - }, - { - "domain":"detail.tmall.hk", - "expirationDate":1678111546, - "hostOnly":true, - "httpOnly":false, - "name":"pnm_cku822", - "path":"/", - "sameSite":null, - "secure":false, - "session":false, - "storeId":null, - "value":"098%23E1hvIQvUvbZvjQCkvvvvvjiWRs5Z1jEjR2dvgj3mPmPZtj1bRsLWgjtPPsdwgjAevpvhvvmv9F9CvvpvvvvvvvhvC9v9vvCvpv9CvhQhRP%2BvClsWafmxdByaWGjxs4hZ%2B3%2Bua4oQD40OwkM6qwVHRfvrcKkxfwkKSeQEfw1lY2Kz8Z0vQbmxdXyaUExr1WkKHkx%2F1W3lYCI7Kvhv8vvvvU1vpvvvvvv2ohCvCVUvvvnUphvp4vvvv63vpCvhvvv2ohCvhvoIvpvUvvmvplsFoXRUvpvjmvmC9cHCtvgCvvpvvvvv" - }, - { - "domain":".tmall.hk", - "hostOnly":false, - "httpOnly":true, - "name":"cookie1", - "path":"/", - "sameSite":"no_restriction", - "secure":true, - "session":true, - "storeId":null, - "value":"BxNXlt0D51JOFzwVkC855E5hMO%2FLJ%2BIjtQKRXjOlrHA%3D" - }, - { - "domain":".tmall.hk", - "hostOnly":false, - "httpOnly":false, - "name":"cancelledSubSites", - "path":"/", - "sameSite":"no_restriction", - "secure":true,t - "session":true, - "storeId":null, - "value":"empty" - }, - { - "domain":".tmall.hk", - "expirationDate":1710079422.393609, - "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":"_l_g_", - "path":"/", - "sameSite":"no_restriction", - "secure":true, - "session":true, - "storeId":null, - "value":"Ug%3D%3D" - }, - { - "domain":".tmall.hk", - "hostOnly":false, - "httpOnly":false, - "name":"_nk_", - "path":"/", - "sameSite":"no_restriction", - "secure":true, - "session":true, - "storeId":null, - "value":"zhangdeman6" - }, - { - "domain":".tmall.hk", - "hostOnly":false, - "httpOnly":false, - "name":"_tb_token_", - "path":"/", - "sameSite":"no_restriction", - "secure":true, - "session":true, - "storeId":null, - "value":"30407d3b645b3" - }, - { - "domain":".tmall.hk", - "hostOnly":false, - "httpOnly":false, - "name":"dnk", - "path":"/", - "sameSite":"no_restriction", - "secure":true, - "session":true, - "storeId":null, - "value":"zhangdeman6" - }, - { - "domain":".tmall.hk", - "expirationDate":1691071546, - "hostOnly":false, - "httpOnly":false, - "name":"l", - "path":"/", - "sameSite":null, - "secure":false, - "session":false, - "storeId":null, - "value":"fB_jjhQqT3bcatTDBOfaFurza77OSIRYYuPzaNbMi9fP_XfB5rWCW6JFWaY6C3GVF602R38_FFteBeYBc3xonxv9G-QXeLHmndLHR35.." - }, - { - "domain":".tmall.hk", - "expirationDate":1707084222.393463, - "hostOnly":false, - "httpOnly":false, - "name":"lid", - "path":"/", - "sameSite":"no_restriction", - "secure":true, - "session":false, - "storeId":null, - "value":"zhangdeman6" - }, - { - "domain":".tmall.hk", - "hostOnly":false, - "httpOnly":false, - "name":"login", - "path":"/", - "sameSite":"no_restriction", - "secure":true, - "session":true, - "storeId":null, - "value":"true" - }, - { - "domain":".tmall.hk", - "hostOnly":false, - "httpOnly":true, - "name":"sgcookie", - "path":"/", - "sameSite":"no_restriction", - "secure":true, - "session":true, - "storeId":null, - "value":"E100RQxy9HrGQ0dqJP7D0skhsmAR0MNIWjOxfE9v%2Booy%2BVv3jquiIls3KiK%2BNCGCVLIO3oLnQg5eHL7p9OxXTQP5zZlMCUWIymMwWp1fGTUu%2F51XqdviOSUH15DdkYu26Isz" - }, - { - "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":"zhangdeman6" - }, - { - "domain":".tmall.hk", - "hostOnly":false, - "httpOnly":false, - "name":"uc1", - "path":"/", - "sameSite":"no_restriction", - "secure":true, - "session":true, - "storeId":null, - "value":"existShop=false&cookie14=UoezSgPKm%2FLtYA%3D%3D&pas=0&cookie16=U%2BGCWk%2F74Mx5tgzv3dWpnhjPaQ%3D%3D&cookie15=V32FPkk%2Fw0dUvg%3D%3D&cookie21=W5iHLLyFfX5Xzx7qNYvXUg%3D%3D" - }, - { - "domain":".tmall.hk", - "hostOnly":false, - "httpOnly":false, - "name":"unb", - "path":"/", - "sameSite":"no_restriction", - "secure":true, - "session":true, - "storeId":null, - "value":"1708690398" - }, - { - "domain":".tmall.hk", - "expirationDate":1675605805, - "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, _ := NewChrome("/Users/zhangdeman/Downloads/chromedriver", 9515, false, nil) instance.Render(cookieList) }