双击嵌套在其他iframe中的iframe中的WebElement

时间:2013-08-09 11:42:43

标签: python python-2.7 selenium selenium-webdriver

基本上,我的页面有以下规格:

<html>
   ...
   <iframe id="lvl1">
       <html>
           <iframe id="lvl2">
               <div>Double Click Me !</div>
           </iframe>
       </html>
   <iframe>
</html>

我无法双击给定元素(使用ActionChain),因为当我执行MoveTargetOutOfBoundsException时会抛出。

    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.common.exceptions import MoveTargetOutOfBoundsException

    try:
        action = ActionChains(context.browser)
        action.double_click(webelement)
        action.perform()
    except MoveTargetOutOfBoundsException:
        context.browser.switch_to_default_content()
        context.browser.switch_to_frame("lvl1")
        context.browser.switch_to_frame("lvl2")
        actions = ActionChains(context.browser)
        my_locationXY = emoji.get_location()
        actions.move_to_element_with_offset(frame, my_locationXY["x"], my_locationXY["y"]).double_click().perform()

我将上一个评论基于here发布的最新评论。

1 个答案:

答案 0 :(得分:0)

上面链接中发布的方法实际上是好的方法。我当前选择的帧不是第n-1帧的问题,我需要显式引用第n帧(我的元素所在的帧)

location = (element.location['x'] + element.size['width'] / 2, element.location['y'] + element.size['height'] / 2)

#Switching to the n-1 th frame, frames is an array of frames' name
frames = page.frame

browser.switch_to_default_content()
for frameName in frames[:len(frames) - 1]:
    browser.switch_to_frame(frameName)

#Handle to the n th frame
frame = browser.find_element_by_tag_name(name="iframe")
action = ActionChains(context.browser)
action.move_to_element_with_offset(frame, location[0], location[1]).double_click()
action.perform()