如何构造一个定位器来识别这个元素?

时间:2018-05-14 11:54:13

标签: selenium-webdriver

这个HTML的XPath / CSS应该是什么?要做MovetoElement,我需要检查这个元素,我尝试使用//a[@class='product-name']但是选择了多个节点。

<a class="product-name" itemprop="url" title="Faded Short Sleeve T-shirts" href="http://automationpractice.com/index.php?id_product=1&controller=product"> Faded Short Sleeve T-shirts </a>

2 个答案:

答案 0 :(得分:2)

该网站有两个主要部分,热门和畅销商品。每个都有您正在使用的定位器的匹配元素。 POPULAR位于UL下,ID为homefeatured。最佳卖家位于UL下,ID为blockbestsellers

另一个问题是,您的定位器正在查找围绕产品图片的A标记和另一个用于产品名称的标记。您可以使用其中之一,因为两者的href相同,但如果需要,您可以指定每个。产品图片链接标有班级product_img_link。产品名称标有类product-name

把这一切放在一起......

热门&gt;产品名称

#homefeatured a.product-name[title='Faded Short Sleeve T-shirts']

热门&gt;产品图片

#homefeatured a.product_img_link[title='Faded Short Sleeve T-shirts']

畅销书&gt;产品名称

#blockbestsellers a.product-name[title='Faded Short Sleeve T-shirts']

畅销书&gt;产品图片

#blockbestsellers a.product_img_link[title='Faded Short Sleeve T-shirts']

注意:只有选中该选项卡后,才能看到POPULAR页面上的元素。 BEST SELLERS选项卡上的元素仍然存在但不可见,如果您尝试与它们进行交互,则会抛出。

答案 1 :(得分:-1)

试试这个:

//a[@title='Faded Short Sleeve T-shirts']
相关问题