【问题标题】:Clicking Multiple Links In Selenium IDE在 Selenium IDE 中单击多个链接
【发布时间】:2014-03-26 23:21:17
【问题描述】:

我是 selenium ide 的新手,我想自动化一些网站。我希望它是这样的。

点击

Click Link 1
do some clicking inside that link
go back to the list of link
Click Link 2
do some clicking inside that link
go back to the list of link
Click Link 3
and so on

我唯一的问题是我不知道它将如何单击顶部的第一个链接。这是网站的html。

<h5>20 seconds ago</h5>
<ul>
<li class="notification-posted">
<img height="15" alt="" src="/assets/images/icons/notification-posted.png">
<a href="/account/54351-wews">wews</a>
send new
<a href="/news/53235">post</a> **Link 1**
</li>
</ul>
<h5>3 minutes ago</h5>
<ul>
<li class="notification-posted">
<img height="15" alt="" src="/assets/images/icons/notification-posted.png">
<a href="/account/632323-yokol">yokol</a>
submitted a new
<a href="/news/253129-loss">post</a> **Link 2**
</li>
</ul>
<h5>4 minutes ago</h5>
<ul>
<h3>6 minutes ago</h3>
<ul>
<h5>7 minutes ago</h5>
<ul>
<h2>8 minutes ago</h2>
<ul>
<li class="notification-posted">
<li class="notification-posted">
<li class="notification-posted">
<li class="notification-posted">
<li class="notification-posted">
<img height="15" alt="" src="/assets/images/icons/notification-posted.png">
<a href="/account/153316-problem">hey</a>
send new
<a href="/news/25151-helloworld">post</a> **link 3**
</li>
</ul>

【问题讨论】:

    标签: html selenium xpath selenium-ide


    【解决方案1】:

    我没用过Selenium ide,但是我用过Selenium Webdriver for python,类似

    你只需要通过css选择器来定位你的元素,特别是结构选择器;如果您必须挖掘大量没有 ID/类的标记,这可能是最简单的方法

    CSS 具有后代选择器和伪元素选择器,它们允许您仅根据特定元素在 DOM 中的位置来定位特定元素,而无需 id 或类

    您可以使用:nth-of-type() psuedo 元素,它根据您传递给它的数字来定位该元素的特定出现

    例如在纯 css 中:

    a:nth-of-type(1)

    将在正文中查找并选择其类型中的第一个 a。如果您改用 2,它将定位到第二次出现的锚点。

    例如,在 selenium.webdriver 中,您可以通过以下方式找到您的元素:

    # ff is the webdriver.Firefox() instance
    
    firstAnchor = ff.find_element_by_css_selector("a:nth-of-type(1)")
    
    secondAnchor = ff.find_element_by_css_selector("a:nth-of-type(2)")
    

    您可以使用它来定位第一个、第二个、第三个等元素。如果您需要基于特定属性值定位元素,还有 css 属性选择器

    ff.find_element_by_css_selector("a[href='/account/54351-wews']")
    

    祝梅恩好运。啦啦啦啦啦嘻嘻

    【讨论】:

    • 谢谢你的回答,那是webdriver的吧?我的问题是 ide :(
    • 如果selenium ide必须使用xpath,可以不指定像“//a[1]”或“//a[2]”这样的锚点吗?像数组一样,它会找到该元素的第 n 次出现,在您的情况下是一个锚
    • 如果链接在同一个 /ul 中,这将起作用,如果是这种情况,xpath 将是这样的 //div[@id='main']/ul[1]/li[ 1][@class='notification-posted']/a[2] //div[@id='main']/ul[1]/li[2][@class='notification-posted']/a [2] 不能点击其他 /ul 中的链接,比如这个链接。 //div[@id='main']/ul[2]/li[@class='notification-posted']/a[2] 无论如何,如果这在 selenium ide 中是不可能的,也许我会使用selenium webdriver 我只是不知道从哪里开始大声笑非常感谢你 chola chola he haw oh yea yea yea
    猜你喜欢
    • 2018-05-31
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 2014-10-06
    相关资源
    最近更新 更多