gpt4 book ai didi

python - Selenium / python : How do I move to a new window with no window name given?

转载 作者:太空宇宙 更新时间:2023-11-04 05:18:56 25 4
gpt4 key购买 nike

我需要切换到单击链接后打开的新窗口。我没有窗口名称,也没有来自链接的目标,因此 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) 时,我的打印语句确认我正在切换到新窗口,但我仍然无法抓取任何元素。我曾尝试通过 xpath、名称和 CSS 选择器来获取它们,但运气为零。这可能是 Selenium 或 Python 绑定(bind)的错误吗?

最佳答案

您的驱动程序 实例应该有一个名为window_handles列表。您应该能够保存当前和所需的窗口句柄,并使用 driver.switch_to_window 在它们之间切换:

main_window = driver.window_handles[0]
new_window = driver.window_handles[1]
driver.switch_to_window(new_window)
# Do something
driver.switch_to_window(main_window)

关于python - Selenium / python : How do I move to a new window with no window name given?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40984195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com