1

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

<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()

我根据此处发布的最后一条评论创建了前面的片段。

4

1 回答 1

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()
于 2013-08-09T15:27:56.580 回答