我是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>
发布于 2014-02-24 17:36:45
我没有使用Selenium,但是我已经为python使用了Selenium,类似的
您只需要通过css选择器来定位您的元素,特别是结构选择器;如果您需要挖掘大量没有id/类的标记,这是最简单的方法。
CSS有子代选择器和psuedo元素选择器,它们允许您仅根据特定元素在DOM中的位置来定位,而不需要id或类。
您可以使用:nth-of-type()
psuedo元素,它根据传递给它的数字来确定该元素的特定出现情况。
例如,在普通css中:
答:第n种类型(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)")
你可以用它来瞄准1,2,3等元素。如果需要基于特定属性值的元素,也有css属性选择器
ff.find_element_by_css_selector("a[href='/account/54351-wews']")
祝你好运梅恩。胆壳
https://stackoverflow.com/questions/22002288
复制