我正试着用硒。问题如下:
文档结构:
<div class="jsSkills oSkills">
<a class="oTag oTagSmall oSkill" href="/contractors/skill/software-testing/" data-contractor="749244">software-testing</a>
<a class="oTag oTagSmall oSkill" href="/contractors/skill/software-qa-testing/" data-contractor="749244">software-qa-testing</a>
<a class="oTag oTagSmall oSkill" href="/contractors/skill/blog-writing/" data-contractor="749244">blog-writing</a>
</div>我需要获得a的所有文本,以便排列如下:
{"software-testing", "software-qa-testing", "blog-writing"}我试过这个:
contrSkill = driver.find_element(:xpath, "//div[contains(@class, 'jsSkills')]").text
puts contrSkill但得到了这个:
"software-testingsoftware-qa-testingblog-writing"请解释如何适当地制作数组。
发布于 2013-11-18 18:30:19
您应该获得所需的所有链接元素(使用find_elements)。然后,您可以遍历每个链接并将其文本收集到一个数组中(Ruby有一个collect方法来帮助实现这一点)。
# Get all of the link elements within the div
skill_links = driver.find_elements(:xpath, "//div[contains(@class, 'jsSkills')]/a")
# Create an array of the text of each link
skill_text_array = skill_links.collect(&:text)
p skill_text_array
#=> ["software-testing", "software-qa-testing", "blog-writing"]https://stackoverflow.com/questions/20053844
复制相似问题