Select Selenium 2.9 中的图像

Select Image in Selenium 2.9

我正在尝试使用 RSpec 来尝试在我实习的 post 维护周末进行自动化测试。我正在使用 Selenium WebDriver 2.9.0 来构建框架,然后添加个性化代码。我目前坚持的是尝试让 WebDriver 单击图像,然后导航到正确的 HTML 但我目前无法这样做。这是我目前所拥有的...

   it "can go to Ultratime" do
     @ie_driver.find_element(:link, "My Resources").click
     wait.until { @ie_driver.execute_script("return document.readyState;") == "complete" }
     sleep 3
     wait.until { @ie_driver.find_element(:link_text => "Login").displayed?}
       #test above line
       puts "found Ultratime"

     #this just finds the "Logout" button and clicks it
     @ie_driver.find_element(:name, "ee").click
   end

这是与上述 "button" 有关的 html 代码,用于我要浏览的网站:

<body id="ultratime-insidend" class="ultratime ultratime-insidend ">
<p id="ultratime">
<a class="single" href="#">
    <img alt="Ultratime" src="https://controller.nd.edu/stylesheets/images/logo2.gif"></img>
    Login
</a>

如有任何帮助,我们将不胜感激!

点击图片就像点击任何其他元素一样。如果您在定位图像时遇到问题,请给它 id 并使用 css 选择器,即

driver.find_element_by_css_selector("#yourimageid").click()

编辑:要将焦点切换到框架,请使用:

driver.switch_to.frame driver.find_element(..)

任何定位器都应该可以工作,如果你不能设置 ID,比如

drive.switch_to.frame driver.find_element( :xpath, "//iframe" )

Clicking on image works just like clicking on any other element. If you have >problem locating image, give it id and use css selector i.e.

driver.find_element_by_css_selector("#yourimageid").click()

Edit: to switch focus to frame, use:

driver.switch_to.frame driver.find_element(..)

Any locator should work, if you can't set ID, like

drive.switch_to.frame driver.find_element( :xpath, "//iframe" )

谢谢 Borsunho,在发现我需要先找到 iframe 之后,事情变得简单多了。这是我的最终结果:

 it "can go to Ultratime", focus: true do
   @ie_driver.find_element(:link, "My Resources").click
   @ie_driver.switch_to.frame(@ie_driver.find_element(:css, 'iframe[src="https://controller.nd.edu/ultratime/insidend/"]'))
   @ie_driver.find_element(:class,"single").click
 end