【发布时间】:2021-03-05 08:21:45
【问题描述】:
在我的previous question 之后,我现在被困在更远的地方......
我点击了一个按钮,使用:
# click on the "continue" button
continue_button = driver.find_element_by_id(elem_id_continue_login)
continue_button.click()
现在我进入了一个新网页。点击几秒钟后,我可以看到驱动程序已更新:
# click on the "continue" button
print(driver.current_url)
continue_button = driver.find_element_by_id(elem_id_continue_login)
continue_button.click()
# WebDriverWait(driver, WEB_DRIVER_WAIT_TIME).until(EC.new_window_is_opened((driver.current_window_handle)))
import time; time.sleep(5)
print(driver.current_url)
# Output:
https://www.bankotsar.co.il/wps/portal/
https://online.bankotsar.co.il/wps/myportal/FibiMenu/Online
但是,驱动程序似乎无法在新网页中找到元素。每次我尝试通过 id 查找元素时,我都会得到
*** selenium.common.exceptions.NoSuchWindowException: Message: Browsing context has been discarded
其中,AFAIK 意味着我正在尝试在死窗口中搜索 - 即前一个窗口。
- (1) 如何将驱动程序移动到正确的位置? (如您所见,我尝试使用
new_window_is_opened,但这对我不起作用)。time.sleep()似乎对driver.current_url有帮助,但仅此而已。driver.switch_to.active_element有帮助 - 但只是有时 - 不知道如何以及为什么...... - 窗口打开后,有时会弹出需要关闭才能继续的弹窗。它有一个关闭按钮+“X”按钮,我希望使用“find_element_by_id”找到它,然后点击它们。
- (2) 弹出窗口的处理方式是否与其他内容不同? (即我可以找到它们并单击,还是应该调用其他方法?)
- (3) 如何知道弹出窗口是否真的弹出?似乎有 80% 的时间会发生这种情况,我不知道为什么
【问题讨论】:
-
点击
continue按钮后是不是打开了一个新的浏览器窗口? -
@KunduK 发布了我的解决方法
-
移动到新窗口
driver.switch_to_window(driver.window_handles[-1]),如果你想回到上一个窗口driver.switch_to_window(driver.window_handles[0]) -
@Kunduk window_handles 是整个脚本运行中大小为 1 的列表
标签: javascript python html selenium