本文介绍了单击按钮后使用 Selenium 切换到新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个非常简单的网页项目,但有点卡住了.我正在使用一个网站,在填写表单并单击按钮后,我会在新窗口中打开 XML 文档,为我提供所需的数据.但是,我不知道如何访问它,因为我没有得到窗口名称

I'm working on quite a simple webpage project and got stuck a bit. I'm using a website that after filling the form and clicking the button gives me my desired data in for of XML document opening in a new window. However, I have no clue how to access it, as I am not given the window name

browser = Browser('firefox')
browser.visit('http://desiredurl/')
form = browser.find_by_id('input')
button = browser.find_by_id('send')
form.fill(string)
button.click()

单击按钮会触发 ajax 请求 (doAjaxRequest("POST", url, xml);) 并打开一个包含 XML 文档的新窗口.从打开的 XML 访问数据的最佳方式是什么?

Clicking on a button triggers an ajax request (doAjaxRequest("POST", url, xml);) and opens a new window with XML document. What is the best way to access data from opened XML?

推荐答案

您的问题的解决方案是将 Explicitwaitexpected_conditions 诱导为 number_of_windows_to_be然后切换到新窗口如下:

The solution for your question will be to induce Explicitwait with expected_conditions as number_of_windows_to_be and then switch over to the new window as follows :

parent = driver.current_window_handle
button = browser.find_by_id('send')
form.fill(string)
button.click()
WebDriverWait(driver, 10).until(
    EC.number_of_windows_to_be(2)
    )
child = driver.window_handles[1]
driver.switch_to_window(child)
print ("Child Window ID is : %s" %child)
print("Child Window Title is : %s " %(driver.title))

这篇关于单击按钮后使用 Selenium 切换到新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 17:33