gpt4 book ai didi

python - 单击按钮后使用 Selenium 切换到新窗口

转载 作者:太空宇宙 更新时间:2023-11-03 14:34:08 49 4
gpt4 key购买 nike

我正在开发一个非常简单的网页项目,但遇到了一些困难。我正在使用一个网站,填写表格并单击按钮后,我会在新窗口中打开 XML 文档,从而获得所需的数据。但是,我不知道如何访问它,因为我没有给出窗口名称

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 访问数据的最佳方式是什么?

最佳答案

您问题的解决方案是使用 expected_conditions 作为 number_of_windows_to_be 引发 Explicitwait,然后切换到新窗口,如下所示:

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

您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

关于python - 单击按钮后使用 Selenium 切换到新窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47082900/

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