C # uses selenium to realize QQ space data crawl login QQ space

Source: Internet
Author: User
Tags xpath


by @ Eat Watermelon Star remind

First we introduce the next selenium

Selenium is also a tool for Web application testing. The selenium test runs directly in the browser, just as the real user is doing. Supported browsers include IE, Mozilla Firefox, Mozilla Suite, and more. The main features of this tool include: test and browser compatibility--test your application to see if it works well on different browsers and operating systems. Test system functionality-Create a regression test to verify software functionality and user requirements. Supports automatic recording actions and automatic generation. Net, Java, Perl, and other language test scripts. Selenium is an acceptance testing tool ThoughtWorks specifically written for Web applications.

Detailed Introduction Click here

Here we take advantage of simulated live operation, not to be in TX harmony for access and data capture

The IDE used here is VS2017, the system Win10, the browser driver chooses Chromedriver

New Unit Test Solution

Installing the NuGet Package

I use Google browser to collect data, so the use of Google Browser driver, if you do not like the Google Browser can reference other driver package

Command

install-Packageselenium.webdriverinstall-package Selenium.WebDriver.ChromeDriver

Or a direct manager installation such as

Selenium.WebDriver.ChromeDriver is to copy the Chorme driver, the ChormeDriver.exe program, into the bin directory after the project is compiled.

Netcore is copied to the corresponding NETCOREAPPV file directory

So the basic reference is complete, the following is the implementation of the specific

First we create a test class Qzone.cs, and all of the calls behind us are tested and implemented in the test class

 Public class qzonetest {        [Fact]        publicvoid  qqlogin ()        {            var New chromedriver ();             " qzone.qq.com/ " ;            Driver. Quit ();        } }

Because it is the Xunit test class, you can directly right-click Debug Test, run up to see

The driver automatically opens the Chrome browser and reverses to the specified page.

The space page has been opened, here is the login step

There are two ways to log in, which is also provided by QQ

The first kind if have already landed QQ, directly click the avatar to be able to log in

The second type of account password login

Let's take a detailed introduction

First, the second, the first is relatively simple, after the second learning, the first kind is easy.

We need to find the text box to enter the account number and password

Here, we're going to use selenium to find the element.

This gives us a lot of ways to find elements, and by name we can see what it means.

var New chromedriver ();d River. Findelement ();d River. Findelementbyclassname ();d River. Findelementbycssselector ();d River. Findelementbyid ();d River. Findelementbyname ();d River. Findelementbytagname ();d River. Findelementbyxpath ()

We use the Byxpath method to find the element, the details of the XPath can be self-aware, I'll just show you how to quickly find the XPath

Just open the space address, locate the text box, and then examine the element, right-side element position right-->copy->copy XPath

Can get an address like this

*[@id = "U"]

Same way to find the password box

*[@id = "P"]

With this XPath we can find the XPath path to the user name and write it in the program

 Try            {                varUserName = driver. Findelementbyxpath ("//*[@id = ' u ')"); //the username here is the text box for the user name.//set the value of the user nameUsername.sendkeys ("123456"); varPWD = driver. Findelementbyxpath ("//*[@id = ' P ']"); Pwd. SendKeys ("********"); }            finally{driver.            Quit (); }

An exception occurred after running

Don't panic, this is because the browser driver did not find the elements caused by, we have to carefully examine

The original text box is nested in an IFRAME, no wonder the current driver cannot find the element, because the current driver will only find the element under the current connection, the nested element is not included

So we're going to use the switching syntax.

 Try            {                //There are two kinds of switching syntax, one is based on the index switch, and the other is switching based on the IFRAME name//Here we use the name switch                itargetlocator tagetlocator = driver.                SwitchTo (); //tagetlocator.frame (1); //Frame index.                tagetlocator.frame ("Login_frame");  //frame frame name.                varUserName = driver. Findelementbyxpath ("//*[@id = ' u ')"); //the username here is the text box for the user name.//set the value of the user nameUsername.sendkeys ("123456"); varPWD = driver. Findelementbyxpath ("//*[@id = ' P ']"); Pwd. SendKeys ("********"); }            finally{driver.            Quit (); }

Then debug the tests, such as

This fills in the value into the text box.

Finally, click Login, find the login button element, click it the same way to find the login button element and click

[Fact] Public voidQqlogin () {
DynamicType = (Newpicturetest ()). GetType (); stringCurrentDirectory =path.getdirectoryname (type. Assembly.location); varDriver =NewChromedriver (currentdirectory); Driver. URL="qzone.qq.com/"; Try { //There are two kinds of switching syntax, one is based on the index switch, and the other is switching based on the IFRAME name//Here we use the name switchItargetlocator Tagetlocator =driver. SwitchTo (); //tagetlocator.frame (1); //Frame index.Tagetlocator.frame ("Login_frame");//frame frame name. varUserName = driver. Findelementbyxpath ("//*[@id = ' u ')"); //the username here is the text box for the user name.//set the value of the user nameUsername.sendkeys ("123456"); varPWD = driver. Findelementbyxpath ("//*[@id = ' P ']"); Pwd. SendKeys ("********"); varBtnlogin = driver. Findelementbyxpath ("//*[@id = ' Login_button ')"); //here is to determine whether the login button is visible, can not write, call the click Method directly if(Btnlogin! =NULL&& btnlogin.displayed = =true) { btnlogin.click (); } } finally{driver. Quit (); } }

And then the login was successful.

Make a summary, use a few key grammar

//iframe Toggle, if the element you want to capture is not on the current page, locate the nested page to toggle//There are two kinds of switching syntax, one is based on the index switch, and the other is switching based on the IFRAME nameItargetlocator Tagetlocator =driver. SwitchTo (); //tagetlocator.frame (1); //Frame index.Tagetlocator.frame ("Login_frame");//frame frame name.//find element methods, you can use CSS to locatevarUserName = driver. Findelementbyxpath ("//*[@id = ' u ')");//sets the value of the text box SendKeysUsername.sendkeys ("123456");//Element Click event varBtnlogin = driver. Findelementbyxpath ("//*[@id = ' Login_button ')"); Btnlogin.click ();

As long as you understand the driver operation of the browser some ways to find out what you want to do, such as click the Switch login box and shortcut login

For example, find the album element and click, find the menu to say the elements and click, set the text box value, Hair said, write a message can be explored using

Log in first talk about here, another day to write about or message board, or album pictures of the Preservation

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.