如何在Robot Framework Selenium中单击元素(html标记)(How to click on an element (html tag) in Robot Framework Se

编程入门 行业动态 更新时间:2024-10-11 21:30:22
如何在Robot Framework Selenium中单击元素(html标记)(How to click on an element (html tag) in Robot Framework Selenium)

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

例如

在文本字段中输入文本,然后输入密钥

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

我的机器人代码

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>

错误

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

Input text in the text field and then pass key enter

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>

Error

最满意答案

请注意,这是一个相当脆弱的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=

如有任何问题请咨询。

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.

更多推荐

本文发布于:2023-07-14 19:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106732.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   标记   元素   如何在   Framework

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!