【问题标题】:How to click on an element (html tag) in Robot Framework Selenium如何单击 Robot Framework Selenium 中的元素(html 标签)
【发布时间】:2017-08-07 08:08:38
【问题描述】:

我正在尝试使用机器人框架在 html 标签上查找元素。我想点击第一个元素,无论元素是什么。

例如

  1. 在文本字段中输入文本,然后按回车键

  2. 该项目将显示,我想点击第一个项目,但我不能

我的机器人代码

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')]

我的 HTML 标签

<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>

错误

【问题讨论】:

  • 也发布您的机器人代码?
  • “我不能”是什么意思?为什么你不能?你打算怎么做?
  • 我刚开始学习机器人框架,我的作业希望我在输入文本进行搜索后出现时选择第一个项目。但我无法选择第一个项目,因为没有任何 id选择 :'(
  • @Goralight 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')]

标签: html robotframework selenium2library


【解决方案1】:

请注意,这是一个相当脆弱的 Xpath,但它会完成您想要完成的工作:

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]

这个机器人代码对我有用,它进入网站,然后在搜索中输入“狗”,然后点击第一个图像结果。 如果您想查找第二个、第三个等 - 更改最后一个数字。例如:

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

将找到第 5 个结果。只需更改[5] 的位置即可。另外,在你的机器人中确保它显示了我刚刚发布的整个 xpath。这包括xpath=

有什么问题欢迎提问。

【讨论】:

  • 非常感谢我可以点击我想要的项目:)))))
  • @iSS.f 没问题,很高兴我能帮上忙 :) 祝你有美好的一天!
  • 我还有一个问题xpath=(//div[@id='wrapper_body_all']/div/div[3]/div/div/div/div[2]/ul/li/div/div/a/img) div后面的[3][2]是什么意思?
  • @iSS.f [3] 基本上意味着找到我3rd 条目。例如,如果有 5 张图片,而您执行 //img[3],它将通过 HTML 文档并找到您的 3rd 条目。您可以对所有 HTML 元素执行此操作。如果你想学习 Xpath,我推荐这个:ricostacruz.com/cheatsheets/xpath.html 它在我刚开始的时候帮助我学习 :)
【解决方案2】:
Sometimes you can use another element earlier or later that does find it, and then use 

/preceding::div[1]

/ascending::div[1]

respectively

【讨论】:

    猜你喜欢
    • 2011-02-18
    • 2021-03-12
    • 2019-05-15
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多