Selenium / Python:如何移动到没有窗口名称的新窗口?

时间:2016-12-05 22:04:33

标签: python selenium automated-tests

我需要切换到单击链接后打开的新窗口。我没有链接中的窗口名称或目标,因此move_to.window()会抛出异常。如何切换到新窗口?

编辑:我正在使用driver.switch_to.window()来获取窗口句柄列表的最后一个索引。 Selenium似乎正在抓住另一个窗口并执行下面的第一个操作,即从下拉列表中选择一个选项。但是,在选择项目之后立即抛出NoSuchElementException,指出我无法找到我刚刚执行操作的元素。这是我的Python代码:

    driver.find_element_by_xpath("//a[@href=\"link_that_opens_other_window\"]").click()

    Select(driver.find_element_by_id("programID")).select_by_value("621")

    driver.find_element_by_xpath("//a[contains(., \"New Schedule Wizard\")]").click()

    config.long_rest() # short_rest() and long_rest() are custom functions I defined to wait for pages to load

    driver.switch_to.window(driver.window_handles[-1])

    config.short_rest()

    # In newly opened window, import old rotations
    Select(driver.find_element_by_xpath("//select[@name = \"rotationsetID\"]")).select_by_value("16")
    Select(driver.find_element_by_xpath("//select[@name = \"scheduleID\"]")).select_by_index(1)
    driver.find_element_by_xpath("//input[@value=\"Next Step >\"]").click()

我得到的错误是:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//select[@name = "rotationsetID"]"}
(Session info: chrome=54.0.2840.99)
(Driver info: chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT 6.1.7601 SP1 x86_64)

我也尝试将新窗口视为一个框架但是也没有用。

编辑:我尝试过的最新代码:

    driver.find_element_by_xpath("//a[@href=\"link_that_opens_other_window\"]").click()

    Select(driver.find_element_by_id("programID")).select_by_value("621")

    driver.find_element_by_xpath("//a[contains(., \"New Schedule Wizard\")]").click()

    config.long_rest() # short_rest() and long_rest() are just custom functions I defined to wait for pages to load

    old_window = driver.window_handles[0]
    new_window = driver.window_handles[-1]

    print "Old: ", type(old_window), old_window
    print "New: ", type(new_window), new_window

    driver.switch_to.window(new_window)

    print "Current window: ", driver.current_window_handle # this line prints out the same value as new_window

    config.short_rest()

    # In newly opened window, import old rotations
    Select(driver.find_element_by_name("rotationsetID")).select_by_value("16")
    Select(driver.find_element_by_xpath("//select[@name = \"scheduleID\"]")).select_by_index(1)
    driver.find_element_by_xpath("//input[@value=\"Next Step >\"]").click()

编辑:这是包含我想要抓取的元素的HTML代码:

<table cellspacing="8" cellpadding="0" border="0">
  <tbody>
    <tr>
      <td>Academic Year:  </td>
      <td>
        <select name="rotationsetID" onchange="selectAcYearSchedule(this);">
          <option value="0">(select academic year)</option>
          <option value="13">July 1, 2013 - June 30, 2014</option>
          <option value="14">July 1, 2014 - June 30, 2015</option>
          <option value="15">July 1, 2015 - June 30, 2016</option>
          <option value="16">July 1, 2016 - June 30, 2017</option>
          <option value="17">July 1, 2017 - June 30, 2018</option>
        </select>
      </td>
    </tr>
    <tr></tr>
    <tr></tr>
  </tbody>
</table>

编辑(12.9.2016):仍然坚持这一点。

我能够抓住旧窗口和新窗口的窗口句柄,并且在使用driver.title切换到新窗口之后我能够获得新窗口的标题。鉴于我知道新窗口打开后的标题,我认为将该标题传递给move_to.window(new_window_title_here)会有效。但是,当我调用该函数时,Selenium会抛出NoSuchWindowException。当我调用switch_to.window(new_window_handle_here)时,我的print语句确认我正在切换到新窗口,但我仍然无法抓取任何元素。我试过用xpath,名字和CSS选择器抓住它们,运气不好。这可能是Selenium或Python绑定的错误吗?

2 个答案:

答案 0 :(得分:1)

您的driver个实例应该有一个名为list的{​​{1}}。您应该能够保存当前和所需的窗口句柄,并使用window_handles

在它们之间切换
driver.switch_to_window

答案 1 :(得分:0)

如果您在点击链接后刚刚打开了新标签页,那么 应该是window_handles列表中的最后一个标签,因此理论上您可以这样做:

driver.switch_to.window(driver.window_handles[-1])

更好的做法是在打开链接之前跟踪第一个窗口句柄,并在打开链接后选择一个新窗口句柄。