简体   繁体   English

Python selenium - 如何切换到下一个 window

[英]Python selenium - How to switch to next window

Requirement is: To login with username/password credentials to the main page.要求是:使用用户名/密码凭据登录到主页。 Once login is done, new page appears.登录完成后,会出现新页面。 In the new page, there is a dropdown button.在新页面中,有一个下拉按钮。 Select a value from dropdown and take snapshot of the page. Select 下拉列表中的值并拍摄页面快照。

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import Select
import time

options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
driver.get('url_to_page')
driver.find_element_by_id("username").send_keys("admin")
driver.find_element_by_id ("password").send_keys("admin")
driver.find_element_by_id("submit").click()   
firefox_elem = driver.find_element_by_tag_name('html') --> login to main page is working fine
******<missing piece: How to select a value from dropdown in next page>******
firefox_elem.screenshot('test.png') 

Below is the button which need to be selected,下面是需要选择的按钮,

<div id="ember8992" class="ember-view btn-group pull-left">

<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" rel="tooltip" data-original-title="" data-bindattr-168="168">
  <script id="metamorph-126-start" type="text/x-placeholder"></script>Last 1 hour<script id="metamorph-126-end" type="text/x-placeholder"></script>
  <script id="metamorph-127-start" type="text/x-placeholder"></script><script id="metamorph-127-end" type="text/x-placeholder">
  </script> &nbsp;<span class="caret"></span>
</button>
<ul class="dropdown-menu">
  <script id="metamorph-137-start" type="text/x-placeholder"></script><script id="metamorph-128-start" type="text/x-placeholder"></script>
    <li><a href="#" data-ember-action="169"><script id="metamorph-138-start" type="text/x-placeholder"></script>Last 1 hour<script id="metamorph-138-end" type="text/x-placeholder"></script></a></li>
  <script id="metamorph-128-end" type="text/x-placeholder"></script><script id="metamorph-129-start" type="text/x-placeholder"></script>
    <li><a href="#" data-ember-action="170"><script id="metamorph-139-start" type="text/x-placeholder"></script>Last 2 hours<script id="metamorph-139-end" type="text/x-placeholder"></script></a></li>
  <script id="metamorph-129-end" type="text/x-placeholder"></script><script id="metamorph-130-start" type="text/x-placeholder"></script>
    <li><a href="#" data-ember-action="171"><script id="metamorph-140-start" type="text/x-placeholder"></script>Last 4 hours<script id="metamorph-140-end" type="text/x-placeholder"></script></a></li>
  <script id="metamorph-130-end" type="text/x-placeholder"></script><script id="metamorph-131-start" type="text/x-placeholder"></script>
    <li><a href="#" data-ember-action="172"><script id="metamorph-141-start" type="text/x-placeholder"></script>Last 12 hours<script id="metamorph-141-end" type="text/x-placeholder"></script></a></li>
  <script id="metamorph-131-end" type="text/x-placeholder"></script><script id="metamorph-132-start" type="text/x-placeholder"></script>
    <li><a href="#" data-ember-action="173"><script id="metamorph-142-start" type="text/x-placeholder"></script>Last 24 hours<script id="metamorph-142-end" type="text/x-placeholder"></script></a></li>
  <script id="metamorph-132-end" type="text/x-placeholder"></script><script id="metamorph-133-start" type="text/x-placeholder"></script>
    <li><a href="#" data-ember-action="174"><script id="metamorph-143-start" type="text/x-placeholder"></script>Last 1 week<script id="metamorph-143-end" type="text/x-placeholder"></script></a></li>
  <script id="metamorph-133-end" type="text/x-placeholder"></script><script id="metamorph-134-start" type="text/x-placeholder"></script>
    <li><a href="#" data-ember-action="175"><script id="metamorph-144-start" type="text/x-placeholder"></script>Last 1 month<script id="metamorph-144-end" type="text/x-placeholder"></script></a></li>
  <script id="metamorph-134-end" type="text/x-placeholder"></script><script id="metamorph-135-start" type="text/x-placeholder"></script>
    <li><a href="#" data-ember-action="176"><script id="metamorph-145-start" type="text/x-placeholder"></script>Last 1 year<script id="metamorph-145-end" type="text/x-placeholder"></script></a></li>
  <script id="metamorph-135-end" type="text/x-placeholder"></script><script id="metamorph-136-start" type="text/x-placeholder"></script>
    <li><a href="#" data-ember-action="177"><script id="metamorph-146-start" type="text/x-placeholder"></script>Custom<script id="metamorph-146-end" type="text/x-placeholder"></script></a></li>
  <script id="metamorph-136-end" type="text/x-placeholder"></script><script id="metamorph-137-end" type="text/x-placeholder"></script>
</ul>
</div>

I tried How to switch to new window in Selenium for Python?我试过How to switch to new window in Selenium for Python? . . But facing issue.但面临问题。

Can someone assist me on how to work on new window?有人可以帮助我处理新的 window 吗?

Based on your problem description, it's hard to tell if you need to use window handles at all for this.根据您的问题描述,很难判断您是否需要为此使用 window 句柄。 You did mention you tried to use driver.window_handles , but all you mentioned was that you were facing an issue.您确实提到过您尝试使用driver.window_handles ,但您提到的只是您遇到了问题。 Since I don't know what that issue is, I am making a few assumptions here.由于我不知道那个问题是什么,所以我在这里做一些假设。 These two lines of code:这两行代码:

driver.find_element_by_id("submit").click()   
firefox_elem = driver.find_element_by_tag_name('html') --> login to main page is working fine
******<missing piece: How to select a value from dropdown in next page>******

do not necessarily mean firefox_elem = driver.find_element_by_tag_name('html') is working as intended.不一定意味着firefox_elem = driver.find_element_by_tag_name('html')正在按预期工作。 You are just searching for top-level html tag, but if driver is focused on the previous window handle, this call will not throw any errors.您只是在搜索顶级html标记,但如果驱动程序专注于之前的 window 句柄,则此调用不会引发任何错误。 It's not really checking for anything here.它并没有真正检查这里的任何东西。

I would use window_handles and switch_to_window to try and switch to your new window that is opened:我会使用window_handlesswitch_to_window来尝试切换到您打开的新 window :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC


# switch to newly opened window -- the index varies based on number of pages opened.
driver.switch_to_window(driver.window_handles[1])

# click button to expand dropdown (wait on it to exist first)
dropdown_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//button[@data-toggle='dropdown']")))
dropdown_button.click()

# click the option with text "Last 1 year" (wait on it to exist first)
dropdown_option = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//ul/li/a[text()='Last 1 year']")))
dropdown_option.click()

This will click the dropdown item with text 'Last 1 year'.这将单击带有文本“过去 1 年”的下拉项。

I would also recommend trying the following:我还建议尝试以下方法:

driver.find_element_by_id("submit").click()   
firefox_elem = driver.find_element_by_tag_name('html') --> login to main page is working

# switch to newly opened window -- the index varies based on number of pages opened.
driver.switch_to_window(driver.window_handles[1])

print(driver.page_source)

to see what driver thinks your page source is.查看驱动程序认为您的页面源是什么。 This will help us determine what the driver is currently focused on, so that we can determine whether or not we need to utilize driver.window_handles and driver.switch_to_window .这将帮助我们确定驱动程序当前关注的内容,以便我们可以确定是否需要使用driver.window_handlesdriver.switch_to_window

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM