Selenium Iwebdriver for Renren album Backup tool in C #

Source: Internet
Author: User
Tags xpath

I used selenium to write a Renren album Backup tool, pro-test pass.

You need to enter your user name, password, and album address.

The code is as follows:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingOpenqa.selenium;usingOpenQA.Selenium.IE;usingOpenQA.Selenium.Interactions;usingOpenQA.Selenium.Support.UI;usingSystem.Threading;usingSystem.IO;usingSystem.Net;namespacerenrenbackup{ Public classProgram {Static voidMain (string[] args) {            //Configuration.Program PP =NewProgram (); stringURL ="http://www.renren.com"; stringUserName ="";
stringPWD =""; stringAlbumurl =""; //Start Backup.Iwebdriver IW =NewInternetexplorerdriver (); IW=pp. Login (URL, pwd, userName, IW); Iw. Navigate (). Gotourl (Albumurl); Pp. Waituntilpageloadedid (IW,"C-b-describe"); List<string> photourls =Newlist<string>(); varPhotos = IW. Findelements (By.classname ("Photo-box")); varCount = IW. Findelement (By.id ("Album-count")). Text; while(Photos. Count ()! =Int32.Parse (count)) { Try{String Setscroll="document.documentelement.scrolltop="+9000; ((Ijavascriptexecutor) IW). Executescript (Setscroll); Thread.Sleep ( the); Photos= IW. Findelements (By.classname ("Photo-box")); } Catch(Exception ex) {Writelog (ex); } } stringvalue; for(inti =0; I < photos. Count (); i++) {Value= IW. Findelements (By.xpath ("//a/img[@class = ' P-b-item ')")) [i]. GetAttribute ("Data-viewer"); Value= value. Split (',')[4]. Split ('"')[3]. Trim (); Photourls.add (value); } pp. WriteToFile (Photourls); stringFilePath; stringFolderPath = @Environment. GetFolderPath (Environment.SpecialFolder.DesktopDirectory) +@"\"+ IW. Findelement (By.id ("Album-name")). Text; if(!directory.exists (FolderPath)) {directory.createdirectory (FolderPath); } for(inti =0; I < Photourls.count (); i++) {FilePath= FolderPath +@"\"+ i +"pic.jpg"; WebClient mywebclient=NewWebClient (); Try{mywebclient.downloadfile (photourls[i], filePath); } Catch(Exception ex) {Writelog (ex); } } } Public voidWriteToFile (list<string>photourls) { stringFile = @Environment. GetFolderPath (Environment.SpecialFolder.DesktopDirectory) +@"\photourls.txt"; if(File.exists (File)) {using(StreamWriter SW =NewStreamWriter (file,true)) { foreach(stringPhotourlinchphotourls) {SW. Write (Photourl+"\ r \ n"); Sw. Flush (); } } } Else{file.create (File). Close (); using(StreamWriter SW =NewStreamWriter (file,true)) { foreach(stringPhotourlinchphotourls) {SW. Write (Photourl+"\ r \ n"); Sw. Flush (); } } } } Public voidWriteToFile (stringv) {stringFile = @Environment. GetFolderPath (Environment.SpecialFolder.DesktopDirectory) +@"\photourls.txt"; if(File.exists (File)) {using(StreamWriter SW =NewStreamWriter (file,true) ) {SW. Write (v+"\ r \ n"); Sw. Flush (); } } Else{file.create (File). Close (); using(FileStream fs =NewFileStream (file, FileMode.Open)) { using(StreamWriter SW =NewStreamWriter (file,true) ) {SW. Write (v+"\ r \ n"); Sw. Flush (); } } } } PublicIwebdriver Login (stringUrlstringPwdstringUserName, Iwebdriver IW) {IW. Navigate (). Gotourl (URL); Waituntilpageloadedid (IW,"Email"); Iw. Findelement (By.id ("Email")). SendKeys (UserName); Iw. Findelement (By.id ("Password")). SendKeys (PWD); Iw. Findelement (By.id ("Login")). Click (); Waituntilpageloadedxpath (IW,"//span[text () = ' My albums ']"); returnIW; } Public voidWaituntilpageloadedid (Iwebdriver IW,stringID) {Try{IW. Findelement (By.id (Id)); } Catch(Exception ex) {Console.WriteLine (ex); Thread.Sleep ( -); Waituntilpageloadedid (IW, id); } } Public voidWaituntilpageloadedxpath (Iwebdriver IW,stringv) {Try{IW. Findelement (By.xpath (v)); } Catch(Exception ex) {Console.WriteLine (ex). ToString ()); Thread.Sleep ( +); Waituntilpageloadedxpath (IW, v); } } Private Static voidWritelog (Exception ex) {stringLogURL = Environment.getfolderpath (Environment.SpecialFolder.DesktopDirectory) +"\\DownloadLog.txt"; if(File.exists (@logUrl)) {using(FileStream fs =NewFileStream (LogURL, Filemode.append)) { using(StreamWriter SW =NewStreamWriter (FS, Encoding.default)) { Try{SW. Write (ex); } Catch(Exception ex1) {writelog (EX1); } finally{SW. Close (); Fs. Close (); } } } } Else { using(FileStream fs =NewFileStream (LogURL, FileMode.CreateNew)) { using(StreamWriter SW =NewStreamWriter (FS, Encoding.default)) { Try{SW. Write (ex); } Catch(Exception ex1) {writelog (EX1); } finally{SW. Close (); Fs. Close (); } } } } } }}

The references are as follows:

Note the properties for setting IEDriverServer.exe are "Copy always".

After running, you will generate a folder on your desktop with the same name as your Renren album, and your photo album is saved below. The desktop will also have a detailed URL list of the downloaded pictures and a log file if an exception is generated.

OK, go home.

Selenium Iwebdriver for Renren album Backup tool in C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.