htmlrobotframeworkselenium2library

How to click on an element (html tag) in Robot Framework Selenium


I'm trying to find an element on html tags using robot framework. I want to click on first element what ever the element is.

For example

  1. Input text in the text field and then pass key enter

  2. The item will show up and i want to click on first item but i cant

My Robot Code

Open Browser    http://www.tarad.com    chrome
Wait Until Element Is Visible    id=keyword
Input Text    id=keyword    dog
Press Key    id=keyword    \\13
Wait Until Element Is Visible    id=wrapper_body_all
Element Should Be Visible    id=wrapper_body_all
Click Element    xpath=//div[id='warpper_all']/div[id='wapper_body_all']//ul/il//img[constains(@src,'//img.trd.cm/120/120/sumpow/img-lib/spd_20140314140410_b.jpg')]

My HTML Tag

<div id="wrapper_body_all"> 
<div class="main-wrap">
    <!--start : #rightProduct-->
    <div class="section-col-right">         
        <!--start : rec_product -->
        <div class="rec_product">
        <!-- end : rec_product -->
        <div class="rec_product">
            <div class="section-products-box">
                <div class="hitproduct-body">
                    <ul class="hitproduct-list list">
                       <!--start : product-list--> 
                        <li>
                            <div class="item-border list">
                                <div class="item-image">
                                        <a href="//www.tarad.com/product/5913180" title="ลำโพงน่ารัก dog">
                                            <img src="//img.trd.cm/120/120/sumpow/img-lib/spd_20140314140410_b.jpg"
                                            alt="ลำโพงน่ารัก dog" data-pagespeed-url-hash="3676782613" 
                                            onload="pagespeed.CriticalImages.checkImageForCriticality(this);">
                                        </a>
                                </div>
                                <div class="item-details">                                          
                                    <div class="product-name">
                                        <a href="//www.tarad.com/product/5913180" title="ลำโพงน่ารัก dog">ลำโพงน่ารัก dog</a>
                                    </div>
                                </div>
                            </div>      
                        </li>
                        <!--end : product-list-->
                        <!--start : product-list--> 
                        <li>
                            <div class="item-border list">
                                <div class="item-image">
                                    <a href="//www.tarad.com/product/6755464" title="Brewdog Magic Stone Dog - 330 ml - 5">
                                        <img src="//www.tarad.com/images/web_main/default150x150.gif" 
                                        alt="Brewdog Magic Stone Dog - 330 ml - 5" data-pagespeed-url-hash="2123596038" 
                                        onload="pagespeed.CriticalImages.checkImageForCriticality(this);">
                                    </a>
                                </div>
                                <div class="item-details">                                          
                                    <div class="product-name">
                                        <a href="//www.tarad.com/product/6755464" title="Brewdog Magic Stone Dog - 330 ml - 5">
                                            Brewdog Magic Stone Dog - 330 ml - 5</a> 
                                    </div>
                                </div>
                            </div>
                        </li>
                        <!--end : product-list-->
                        ....
                        ....
                        ....
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>

strong text

Error enter image description here


Solution

  • Be warned, this is quite a brittle Xpath, but it will do the job you're trying to achieve:

    Open Browser    http://www.tarad.com    chrome
    Wait Until Element Is Visible    id=keyword
    Input Text    id=keyword    dog
    Press Key    id=keyword    \\13
    Wait Until Element Is Visible    id=wrapper_body_all
    Element Should Be Visible    id=wrapper_body_all
    Wait Until Page Contains Element    xpath=(//div[@id='wrapper_body_all']/div/div[3]/div/div/div/div[2]/ul/li/div/div/a/img)[1]
    Click Image    xpath=(//div[@id='wrapper_body_all']/div/div[3]/div/div/div/div[2]/ul/li/div/div/a/img)[1]
    

    This Robot Code works for me, it goes to the site, then inputs "dog" to the search, and then clicks on the very first image result. If you want to find the 2nd, 3rd, etc - change the last number. for example:

    xpath=(//div[@id='wrapper_body_all']/div/div[3]/div/div/div/div[2]/ul/li/div/div/a/img)[5]
    

    Will find the 5th result. just change where it says [5]. Also, in your Robot make sure it says the entire xpath I just posted. That includes the xpath=

    Any questions please ask.