ruby - selenium-webdriver 点击第一个链接

标签 ruby selenium-webdriver

我正在使用 selenium-webdriver,我正在寻找一些非常简单的东西,但我没有在文档中找到它。

这是我的代码的一部分:

browser = Selenium::WebDriver.for :firefox, :profile => profile
browser.navigate.to 'an_url'
# I find ancestor element
browser.find_element(:id, "displaylinks").find_element(:id, "link0").find_element(:class, "link-center")
# but I want to click the first link child of this last element

有什么想法吗?

最佳答案

您可以使用几个不同的选择器:

ancestor_element = browser.find_element(:id, "displaylinks").find_element(:id, "link0").find_element(:class, "link-center")

#Using tag_name
ancestor_element.find_element(:tag_name, 'a').click

#Using css-selector
ancestor_element.find_element(:css, 'a').click

#Using xpath-selector (direct child)
ancestor_element.find_element(:xpath, './a').click 

#Using xpath-selector (anywhere in ancestor)child)
ancestor_element.find_element(:xpath, './/a').click

关于ruby - selenium-webdriver 点击第一个链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13338378/

相关文章:

ruby - 如何在 Ruby 中获取字符串匹配正则表达式的特定部分?

html - 使用 Rails 3 的两列 HTML 表格

ruby-on-rails - 如果还有错误

python - pip安装selenium权限错误

maven - 获取解析器错误 : inconsistent cell count within the table - while running tests in Parallel - Selenium Cucumber Maven framework with Junit

ruby - 从列表中获取最新和最旧的日期 - Ruby

python - 在 Selenium Python 中检查网站的 .readyState 的正确语法是什么?

Python Selenium - 纯粹基于其基于主体元素偏移量的位置点击元素

javascript - Mocha 测试框架的 test.describe 返回值

ruby-on-rails - RoR 从 ActiveRecord::Relation 中获取属性