Selenium positioning elements

Source: Internet
Author: User
Tags glob

Commands (command)

    • Action
      Working with the current state
      When failed, stop the test
    • Assertion
      Verify that the correct value is generated
    • Element Locators
      Specify an element in the HTML
    • Patterns
      For pattern matching

1. Element Locators (Elemental Locator)

    • Id=id
      ID Locator An element that specifies a unique ID in HTML
    • Name=name
      Name locator The first element in the element that specifies the same name in the HTML
    • Identifier=id
      Identifier Locator first looks for the element that has the ID for the HTML, and if not, finds the first element of that name
    • Dom=javascriptexpression
      Dom Locator uses JavaScript expressions to locate elements in HTML, and note that you must start with "document"
      For example:
      dom=document.forms[' MyForm '].mydropdown
      DOM=DOCUMENT.IMAGES[56]
    • Xpath=xpathexpression
      XPath locator uses an XPath expression to locate elements in the HTML, and you must be careful to start with "//"
      For example:
      xpath=//img[@alt = ' The image alt text ']
      xpath=//table[@id = ' table1 ']//tr[4]/td[2]
    • Link=textpattern
      Link Locator Use link to select a connection or anchor element in HTML
      For example:
      Link=the link text
    • In the absence of locator pre-order without a locator prefix, Selenium uses:
      If you are using "document." First, the default is to use the DOM locator, and if it starts with "//", the XPath locator is used by default, and the rest is considered identifier locator

2. String Matching Patterns (string match pattern)

    • Glob:patthern
      Glob mode, using the wildcard character "*" for any length character, "?" Represents a character
    • Regexp:regexp
      Regular expression pattern, matching strings in the form of JavaScript regular expressions
    • Exact:string
      Exact match pattern, exact match of entire string, cannot be used with wildcard characters
    • Selenium uses Golb match mode by default when no string match is specified in the pre-order

3. Select option Specifiers (select options selector)

    • Label=labelpattern
      Specify options by matching text in options
      Example: Label=regexp:^[oo]ther
    • Value=valuepattern
      Specify options by the values in the match options
      Example: Value=other
    • Id=id
      Specify options by the ID of the matching option
      Example: Id=option1
    • Index=index
      Specify options with the ordinal of the matching option, starting with 0
      Example: index=2
    • The default is the text of the matching option in the absence of an option to select the Pre-order
Actions

Describes the actions that users will make.
There are two forms of action: Action and actionandwait, action is executed immediately, and Actionandwait assumes that it takes a long time to get the action's response, while the open will automatically handle the wait time.

    • Click
      Click (elementlocator)
      -click the Connect, button, check and radio boxes
      -If you need to wait for a response after clicking, use "Clickandwait"
      -If you need to go through the JavaScript Alert or confirm dialog to continue, you need to call verify or assert to tell selenium what you expect to do with the dialog box.
      Click Acheckbox
      Clickandwait Submitbutton
      Clickandwait

      Anylink

Selenium. Click ("Id=login");

    • Open
      Open (URL)
      -Open the URL in the browser, you can accept the relative and absolute path of two forms
      -Note: The URL must be within the same security limits as the browser
      Open /mypage
      Open http://localhost/

Selenium. Open ("/");

    • Type
      Type (inputlocator, value)
      -Simulate the input process of the manual and enter values into the specified input
      -also suitable for assigning values to check and radio boxes
      -In this case, only the checked box is assigned a value, note, instead of rewriting its text
      Type NameField John Smith
      Typeandwait Textboxthatsubmitsonchange NewValue

Selenium. Type ("Id=username", UserName);

    • Select
      Select (Dropdownlocator, Optionspecifier)
      -Select a drop-down menu option according to the Optionspecifier option selector
      -If there are more than one selector, such as in wildcard mode, such as "f*b*", or if more than one option has the same text or value, the first match to the value will be selected
      Select DropDown Australian Dollars
      Select DropDown Index=0
      Selectandwait Currencyselector Value=aud
      Selectandwait Currencyselector Label=auslian D*rs
    • Goback,close
      GoBack ()
      Analog Click on the browser's Back button

Selenium. GoBack ();

    • Close ()
      Analog Click the browser Close button


Selenium. Close ();

  • Selectwindow
    Select (Windowid)
    -Select a pop-up window
    -When the window is selected, all commands will be transferred to that window for execution.
    Selectwindow Mypopupwindow
    Selectwindow Null
  • Pause
    Pause (millisenconds)
    -Pauses selenium script execution based on specified time
    -Commonly used when debugging scripts or waiting for a server segment response
    Pause 5000
    Pause 2000
  • FireEvent
    fireEvent (elementlocatore,evenname)
    Simulates the processing action of a page element event being activated
    FireEvent TextField Focus
    FireEvent DropDown Blur
  • Waitforcondition
    Waitforcondition (Javascriptsnippet,time)
    -Wait for a piece of JavaScript code to return a true value within a limited time, and stop waiting for a timeout
    Waitforcondition var value=selenium.gettext ("foo"); Value.match (/bar/); 3000
  • waitforvalue
    waitforvalue (inputlocator, value)
    -wait for an input such as hidden Input) is given a value,
    -the value will be detected in turn, so be aware that if the value has not been given this input for a long time, it may cause blocking
    waitforvalue finishindication isfinished
    & nbsp;    
  • store,strorevalue
    Store (Valuetostore, variablename)
    Save a value into the variable.
    The value can be either combined from another variable or assigned to a variable by a JavaScript expression
    stor E Mr John Smith fullname
    store $.{title} $.{firstname } $.{suname} fullname
    store javascript.{math.round (math.pi*100)/10 0} PI
    storevalue inputlocator variableName

    Saves the value in the specified input to a variable

    Storevalue UserName UserID
    type userName $.{userid}
  • Storetext, Storeattribute
    Storetext (Elementlocator, VariableName)
    Assigns the text value of the specified element to the variable
    Storetext currentdate Expectedstartdate
    Verifyvalue StartDate $.{expectedstartdate}

    Storeattribute (. {}[email protected],variablename.{})
    Assigns the value of the attribute of the specified element to the variable

    Storeattribute [Email protected] ClassOfInput1
    Verifyattribute [Email protected] $.{CLASSOFINPUT1}

Storeattribute TARGET:AA@BB Storage element value: variable stores bb values of AA into variables

String taskname = Selenium. GetAttribute ("[email protected]");
Console.WriteLine (TaskName);

Store the Defvalue value with ID minblogbody in TaskName and print TaskName

Stortetext

target://*[@id = ' UL_80185794-3209-4E6B-8CEA-AF39348C5CDD ']/li[1]/span[6]/a[1]

Value:bind

Echo

${bind}

Save the value of the path to target in the bind variable and output the bind value.

    • Choosecancel., answer.
      Choosecancelonnextconfirmation ()
      -When the next JavaScript pops up the Confirm dialog box, let Selenium select Cancel
      -If you do not have the command, you encounter the Confirm dialog box selenium returns True by default, such as manually selecting the OK button
      Choosecancelonnextconfirmation

      -If the command has already been run, the next time the Confirm dialog box appears, you will again select Cancel
      Answeronnextprompt (answerstring)
      -When the next JavaScript pops up the prompt prompt, give its anweerstring value and select OK

      Answeronnextprompt Kangaroo
Assertions

Allows the user to check the current state. Two modes: Assert and Verify, exit the test when assert fails, and the test will continue to run when Verify fails.

  • Assertlocation, Asserttitle
    Assertlocation (relativelocation)
    Judging is currently on the right page
    Verifylocation /mypage
    Assertlocation /mypage
  • Asserttitle (Titlepattern)
    Check if the title of the current page is correct
    Verifytitle My Page
    Asserttitle My Page
  • assertvalue
    assertvalue (inputlocator, ValuePattern)
    -Check the value of input
    - For checkbox or radio, if selected, the value is "on" and vice versa "off"
    ver Ifyvalue nameField John Smith
    assertvalue document.fo Rms[2].namefield John Smith
  • assertselected, assertselectedoptions
    assertselected (Selectlocator, Optionspecifier)
    Checks whether the selected selection in the drop-down menu of select is the same as the option for Optionspecifer (select selection selector)
    verifyselected dropdown2 John Smith
    verifyselected dorpdown2 value=js*123
    Assertsele CTED document.forms[2].dropdown label=j*smith
    assertselected Document.forms[2].dropdown index=0
  • Assertselectoptions (Selectlocator, Optionlabellist)
    -Check whether the text of the option in the drop-down menu is the same as Optionlabellist
    -Optionlabellist is a comma-separated string
    Verifyselectoptions Dropdown2 John Smith,dave Bird
    Assertselectoptions Document.forms[2].dropdown Smith,j,bird,d
  • Asserttext
    Asserttext (Elementlocator,textpattern)
    -Check the text of the specified element
    -Takes effect only on elements that contain text
    -for Mozilla Type Browser, use Textcontent to take element text, for IE type browser, use InnerText to take element text
    Verifytext StatusMessage Successful
    Asserttext div[@id = ' foo ']//h1 Successful
  • Asserttextpresent, Assertattribute
    Asserttextpresent (text)
    Checks whether the specified text appears on the page currently displayed to the user
    Verifytextpresent Logged in
    Asserttextpresent Logged in

Selenium. Istextpresent ("Beisen: Sharing Test 01")

  • Assertattribute (. {}[email protected]{} , ValuePattern)
    Checks the value of the property of the currently specified element
    verifyattribute [email protected] bigandblod
    assertattribute Docu Ment.images[0] @alt alt-text
    verifyattribute //img[@id = ' foo ']/alt alt-text
  • asserttextpresent, etc.
    asserttextpresent (text)
    asserttextnotpresent (text)
    Assertelementpresent (elementlocator) verifyelementpresent
    Submitbutton  
    assertelementpresent //img[@alt = ' foo ']   assertelementnotpresent (elementlocator)
  • Asserttable
    Asserttable (celladdress, ValuePattern)
    -Check the value in a cell in the table
    -Celladdress's syntax is tableName.row.column, note that the row and column numbers start at 0.
    Verifytable mytable.1.6 Submitted
    Asserttable results0.2 13
  • Assertvisible, nonvisible
    Assertvisible (Elementlocator)
    -Checks whether the specified element is visible
    -Hide an element can be set with CSS ' visibility ' property for ' hidden ', or you can set ' Display ' property to ' none '
    Verfyvisible Postcode
    Assertvisible Postcode
  • Assertnotvisible (Elementlocator)
    Verfynotvisible Postcode
    Assertnotvisible Postcode
  • Editable, non-editable
    Asserteditable (Inputlocator)
    Checks whether the specified input can be edited
    Verifyeditable Shape
    Asserteditable Colour
  • Assertnoteditable (Inputlocator)
    Checks whether the specified input cannot be edited
  • Assertalert
    Assertalert (Messagepattern)
    -Check if JavaScript has an alert dialog box with the specified message
    -The order of alert generation must be consistent with the Order of inspection
    -When alert is checked, it produces the same effect as clicking the ' OK ' button manually. If an alert is created and you do not check it, selenium will make an error in the next action.
    -Note: Selenium does not support JavaScript calling alert () in the OnLoad () event, in which case Selenium requires you to manually click OK.
  • Assertconfirmation
    Assertconfirmation (Messagepattern)
    -Check if JavaScript has a confirmation dialog box with the specified message and alert case, the confirmation dialog must also be checked when they are generated
    -By default, selenium will let confirm () return true, equivalent to the effect of manually clicking the OK button. You can use the Choosecancelonnextconfirmation command to get confirm () returns FALSE. Similarly, if a cofirmation dialog box appears, but you do not check it, selenium will make an error in the next action
    -Note: In a selenium environment, the confirmation dialog box will no longer appear with an explicit popup dialog
    -Note: Selenium does not support invoking the confirmation dialog box in the OnLoad () event, in which case the Display Confirmatioin dialog box appears and requires you to manually click on it.
  • Assertprompt
    Assertprompt (Messagepattern)
    -Check if JavaScript has a prompt dialog box that produces a specified message
    -you check the order of the prompt prompt dialog box must produce the same order
    -The answeronnextprompt command must be called before verifyprompt
    -If the prompt dialog box appears but you do not check, then selenium will error in the next action
    Answeronnextprompt Joe
    Click Id=delegate
    Verifyprompt Delegate to Who?
Parameters and Variables

The Declaration range of parameters and variables is assigned by a simple assignment to a JavaScript expression.
Store,storevalue and Storetext Save the value for the next visit.
Inside the selenium, a map called Storevars is used to save the variable name.

    • Variable Substitution variable substitution
      provides an easy way to access variables, syntax $.{xxx}
      store Mr title
      storeval UE NameField surname
      store $.{title} $.{suname} FullName
      type textelement full name is: $.{fullname}
    • JavaScript Evaluation javascript Assignment
      You can use JavaScript to build any value you need.
      This parameter starts with JavaScript and the syntax is javascript.{' with a trailing '}.
      You can assign a value to an element through a JavaScript expression.
      Store javascript.{' Merchant ' + (new Date ()). GetTime ()} Merchantid
      Type TextElement javascript.{storedvars[' Merchantid '].touppercase ()}
      Source: >  

Selenium positioning elements

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.