我正在使用 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/