Input box three kinds of input method (selenium Webdriver dry)

Source: Internet
Author: User
Tags xpath

On the Ticket booking page, enter the departure city and arrival city input box, found that the direct use of SendKeys,

Most of the situation is entered into a city after not input, after a few days of research, found that can take three ways:

1. Click the input box, and then click the City selection box.

2. Slowly enter the city's abbreviated letter or the city's name section, which will display a drop-down list of cities to choose from, and select the appropriate city from the drop-down list.

3. Directly execute the JS script to the value of input set to the desired values

First, let's say a third way:

    Javascriptexecutor js = (javascriptexecutor) driver;        Js.executescript ("arguments[0].value=\", "Beijing", "from_inpox");

The best performance,

22:35:34.885 info-executing: [Execute script:arguments[0].value= "Beijing", [[[Ch]
Romedriver:chrome on XP (6452a4a961be7bffa2af9d1b63f3d111)], XPath://div[@id
= ' js_flighttype_tab_domestic ']//input[@name = ' fromcity ']])

As demonstrated, both methods are the user's true behavior.

Take the first approach:

    • First, navigate to the input box
    • Click on the Input box
    • Click on the city you want from the pop-up City box
Webelement from_inpox = driver.findelement (By.xpath ("//div[@id = ' js_flighttype_tab_domestic ']//input[@name = ' Fromcity '] "new Actions (driver); Actions.movetoelement (from_inpox). Click (). Perform ();d river.findelement ( By.xpath ("//div[@data-panel= ' domesticfrom-flight-hotcity-from ']//a[@class = ' js-hotcitylist ' and text () = ' XI ' an ']"  ). Click ();   

I did not use the click directly here, but I used the actions because I found that I often reported that element can ' t be clicked such errors when I arrived at the city.

The main idea is that when you want to click to reach the city input box, it is actually obscured by the upper element, and cannot use the click Method, but you can use the Movetoelement method of the actions to click

Or take a scroll to that element, call JS

Javascriptexecutor JSE = (javascriptexecutor) Driver;jse.executescript ("Arguments[0].scrollintoview ()", Element );

The click operation is then available.

If you use the second method, you will encounter a big problem:

How to navigate to the city of JS generated drop-down list? Firebug the list disappears before you locate it!

Looks difficult, repeated attempts to do nothing, and finally suddenly remembered that since it is JS generated, why not use the browser's JS debug function, set breakpoints step-by-step

Sure enough, charm. Nice job~

Idea has, with me to do, open firebug, switch to the "script" interface, first enter the single letter s in the input box, to eject the drop-down list, click the Insert Breakpoint action on the left

You will find that the dropdown box is frozen, good yo, then switch to the HTML interface to locate.

Not only to which network, like Baidu input box can also take such a method, JS set breakpoints, JS pop-up box, pop-up menu will be frozen.

The next step is to select the desired city from the drop-down menu:

        From_inpox.clear ();        From_inpox.sendkeys ("BJ");        Thread.Sleep (8000);        by bj=new By.byxpath ("//div[@class = ' qcbox-fixed js-suggestcontainer ']//td[contains (text (), ' Beijing ')]");        if (iselementpresent (driver,bj,20)) {driver.findelement (BJ). Click ();}     

Note that the drop-down menu may not pop up so fast, need to do a wait, in the selection of the drop-down menu needs to make a judgment, of course, this method is to use webdriverwait

/***@authorYoung *@paramDriver *@paramby *@paramTimeOut *@return*/PublicStaticboolean iselementpresent (webdriver driver, final by By, int TimeOut) {webdriverwait wait = new webdriverwait (driver, timeOut); boolean isPresent = false new expectedcondition< Webelement> () {@Override public Webelement apply (Webdriver d) {return D.findelement ( by); }}). isdisplayed (); return isPresent;}       

is still not perfect, why say, if the element does not appear, not return false but directly throw the exception, is not expected, so modify to Findelements

If not found, the returned list length must be 0, which in turn returns false instead of throwing an exception

/***@authorYoung *@paramDriver *@paramby *@paramTimeOut *@return*@throwsInterruptedexception*/public static boolean Iselementpresent (webdriver driver, final by, int timeOut) throws interruptedexception {boolean isPresent = false); List<webelement> we = driver.findelements (by); if (We.size ()! = 0truereturn isPresent;}       

Test steps:

1. Select Departure City, Beijing

Arrival City-Shanghai

Choose seven days after today

Click the Search button

2. Select an order with "taxes on every flight"

PublicStaticvoid Main (string[] args)Throwsinterruptedexception {Webdriver Driver =Driverfactory.getchromedriver (); Driver.get ("http://flight.qunar.com/"); Driver.manage (). window (). Maximize ();Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds);Driver.manage (). Timeouts (). Pageloadtimeout (30, timeunit.seconds); Webelement from_inpox =Driver. Findelement (by. XPath ("//div[@id = ' js_flighttype_tab_domestic ']//input[@name = ' fromcity ']")); Webelement to_inpox =Driver. Findelement (by. XPath ("//div[@id = ' js_flighttype_tab_domestic ']//input[@name = ' tocity ']")); Webelement from_date =Driver. Findelement (by. XPath ("//div[@id = ' js_flighttype_tab_domestic ']//input[@name = ' fromdate ']")); Webelement Siglewaycheckbox =Driver. Findelement (by. XPath ("//div[@id = ' js_flighttype_tab_domestic ']//input[@class = ' Inp_chk Js-searchtype-oneway '] "));if (!Siglewaycheckbox.isselected ()) {Siglewaycheckbox.click ();} from_inpox.clear (); From_inpox.sendkeys ("BJ"); Thread.Sleep (8000); by BJ =NewBy.byxpath ("//div[@class = ' qcbox-fixed js-suggestcontainer ']//td[contains (text (), ' Beijing ')]");if (Iselementpresent (Driver, BJ, 20) {driver.findelement (BJ). Click ();} to_inpox.clear (); To_inpox.sendkeys ("SH"); Thread.Sleep (8000); by sh =NewBy.byxpath ("//div[@class = ' qcbox-fixed js-suggestcontainer ']//td[contains (text (), ' Shanghai ')]");if (iselementpresent (driver, SH, 20) {driver.findelement (SH). click ();}//actions = new actions (driver);//Actions.movetoelement (from_inpox). Click (). Perform ();//Driver.findelement (//By.xpath ("//div[@data-panel= ' Domesticfrom-flight-hotcity-from ']//a[@class = ' js-hotcitylist ' and text () = ' XI ' an '))//. Click ();//Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds);//Driver.manage (). Timeouts (). Pageloadtimeout (Timeunit.seconds);//Actions.movetoelement (to_inpox). Click (). Perform ();//Driver.findelement (//By.xpath ("//div[@data-panel= ' domesticto-flight-hotcity-to ']//a[@class = ' js-hotcitylist ' and text () = ' Beijing ') ')//. Click ();//Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds);//Driver.manage (). Timeouts (). Pageloadtimeout (Timeunit.seconds);From_date.clear (); From_date.sendkeys (Getdateaftertoday (7)); Webelement search =Driver. Findelement (by. XPath ("//div[@id = ' js_flighttype_tab_domestic ']//button[@class = ' Btn_search ']")); Search.submit (); Driver.manage (). Timeouts (). Implicitlywait (30, timeunit.seconds); Driver.manage (). Timeouts (). Pageloadtimeout (30, timeunit.seconds); Webelement Page2 =Driver.findelement (by. XPath ("//div[@id = ' Hdivpager ']/a[@value = ' 2 ']")); Javascriptexecutor JSE =(javascriptexecutor) driver; Jse.executescript ("Arguments[0].scrollintoview ()", Page2); Page2.click (); Driver.manage (). Timeouts (). Implicitlywait (30, timeunit.seconds); Driver.manage (). Timeouts (). Pageloadtimeout (30, timeunit.seconds); Driver.findelement (By.xpath ("//div[@class = ' Avt_trans ']//p[contains (text (), ' taxes on each flight ')]/ancestor::d iv//div[@ class= ' a_booking ']/a) [3] "). Click (); Driver.findelement (By.xpath ("//div[@id = ' flightbarXI883 ']//div[@class = ' T_BK ']/a"). Click (); }PublicStatic String Getdateaftertoday (IntDateaftertoday) {Calendar cal =Calendar.getinstance (); Cal.add (Calendar.date, +Dateaftertoday); System.out.println (Cal.gettime (). toString ()); Date Date =Cal.gettime (); SimpleDateFormat DF =New SimpleDateFormat ("Yyyy-mm-dd"); System.out.println (Df.format (date));ReturnDf.format (date); }/***@authorYoung *@paramDriver *@paramby *@paramTimeOut *@return*@throwsInterruptedexception*/public static boolean Iselementpresent (webdriver driver, final by, int timeOut) throws interruptedexception {boolean isPresent = false); List<webelement> we = driver.findelements (by); if (We.size ()! = 0truereturn isPresent;}       

The effect is as follows:

    

Reprint Please specify source: http://www.cnblogs.com/tobecrazy/

Input box three kinds of input method (selenium Webdriver dry)

Related Article

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.