click <element>

click on <element>, where element can be xpath, css selector or image

  1. You can click using xpath.
  2. For example, there is a Search button on the top of this page. To click on this button, you can use any of the following:

    click //button[.="Search"]
    click //button[@class="button hidden-navcollapse btn btn-primary"]
    click //input[@name="q"]/following-sibling::button
    • As you can see, there are many ways to specify the xpath of an element.
    • The first one //button[.="Search"] goes by the name of the button. If there is only one button called "Search", then this is ok. But if there are more than one buttons with the name "Search", or if your website supports multiple languages, then this method is not suitable.
    • The second one //button[@class="button hidden-navcollapse btn btn-primary"] goes by the class name of the button. Note that in css, id is unique, but class name are not unique. So if there are more than one buttons with the same class name, then this is not suitable.
    • The third one //input[@name="q"]/following-sibling::button uses the search field //input[@name="q"] as the anchor, and find the button that is directly next to it. This method is very useful when you cannot find a unique way of identifying the button. You can pick one element around the button as the anchor to pinpoint the button.
  3. You can click using css selector.
  4. Using the same example as (1) above, to click the Search button at the top of this page, you can also use the css selector:

    click .hidden-navcollapse.btn-primary
    • The css selector for the Search button is .hidden-navcollapse.btn-primary
    • The effect is the same as using xpath, just different ways of pinpointing the button.
  5. There are indeed some websites that try to stop people from using robots to get information from their websites. In this case, you will find that using xpath or css selector (methods 1 and 2 above) will not work. As a last resort, you can try this image-based method
  6. TagUI has built-in integration with Sikuli (based on OpenCV) to allow identification of web elements and desktop user interface elements for interaction.

    First of all, make sure that you have Sikuli setup and integrated with TagUI. You can refer to this article for details: Setting up Sikuli to work with TagUI on Windows

    Once you have Sikuli set up, you can click the Search button at the top of this page with this code:

    click search_button.png
    • button.png is the image of the button as shown below
    • search button

    • Note that for Sikuli to work, the image has to be in .png format.
    • The image must be stored in the same folder where you run the script.
    • Suppose you stored button.png in a subfolder within the folder where you run your script, you can use relative path to refer to the image as follows:
      click image/click search_button.png

Comments (0)

There are no comments posted here yet

Leave your comments

Posting comment as a guest. Sign up or login to your account.
Attachments (0 / 3)
Share Your Location