【问题标题】:How to update driver location after button click changed url so the driver could find elements按钮单击更改 url 后如何更新驱动程序位置,以便驱动程序可以找到元素
【发布时间】: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


【解决方案1】:

找到了 (1) 的解决方法,但它很奇怪...

首先,在我点击继续按钮后,url 确实发生了变化,但驱动程序没有。

我发现运行以下命令确实会切换驱动程序,但不能开箱即用driver.switch_to.active_element

在运行此之前,我必须运行以下命令driver.switch_to.window(driver.current_url)

但是,此命令引发 NoSuchWindowException,所以我的解决方法是:

 try:                                                                                                                                                                                                        
     driver.switch_to.window(driver.current_url)                                                                                                                                                             
 except NoSuchWindowException:                                                                                                                                                                               
   pass                                                                                                                                                                                                    
                                                                                                                                                                                                             
 driver.switch_to.active_element 

或者使用contextlib.suppress

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 2018-07-24
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    相关资源
    最近更新 更多