0

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.

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?. But facing issue.

Can someone assist me on how to work on new window?

Nirmal Ram
  • 1,180
  • 2
  • 9
  • 18
  • Is a new, physical browser window being opened, or are you just getting redirected to another page once you click submit button? – CEH Nov 22 '19 at 18:45

1 Answers1

1

Based on your problem description, it's hard to tell if you need to use window handles at all for this. You did mention you tried to use driver.window_handles, but all you mentioned was that you were facing an issue. 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. 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. 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:

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'.

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.

CEH
  • 5,701
  • 2
  • 16
  • 40
  • Thanks @christine. print(driver.page_source) did the trick. It was in same page. No need to use window_handles. But i have multiple button in the page. Is there a way to get the button id based on
    ?
    – Nirmal Ram Nov 23 '19 at 03:04
  • 1
    dropdown_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//*[contains(@id,'ember9303')]/button[@data-toggle='dropdown']"))) This worked. Thanks a lot. – Nirmal Ram Nov 23 '19 at 03:35