1

I am using selenium to login to a webpage and take a screenshot post login. Here is my code :

import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("some website")
user_name = driver.find_element_by_name("USERNAME")
password = driver.find_element_by_name("PIN")

user_name.clear()
user_name.send_keys("username")

password.clear()
password.send_keys("password)

###Sends enter
password.send_keys(Keys.RETURN)

# Wait for some time for the next page to load
wait = WebDriverWait(driver, 10)

driver.save_screenshot('screenshot.png')
driver.quit()

When I do this, I get the screenshot of the login page instead of the next page.

The expected output ideally should be a screenshot of the page, post login. I have manually verified by monitoring that selenium opens the webpage, logins, within 10 seconds.

So I did something else, i inserted sleep (10) instead of driver.wait, like this :

wait = WebDriverWait(driver, 10)                   

replaced with

os.sleep(10)

On doing this, I get the screenshot of the next page.

I then also tried to insert a custom wait condition, and searched for an element in the next page :

try:
    element = WebDriverWait(driver, 100).until(
        EC.presence_of_element_located((By.ID, "crnum"))
    )

However, selenium is giving me the following error :

TimeoutException                          Traceback (most recent call last)
<ipython-input-39-16f1ee0dd7f3> in <module>()
     15 try:
     16     element = WebDriverWait(driver, 100).until(
---> 17         EC.presence_of_element_located((By.ID, "crnum"))
     18     )
     19 finally:

D:\anaconda\lib\site-packages\selenium\webdriver\support\wait.py in until(self, method, message)
     78             if time.time() > end_time:
     79                 break
---> 80         raise TimeoutException(message, screen, stacktrace)
     81 
     82     def until_not(self, method, message=''):

TimeoutException: Message: 

I have checked the second page , and the id exists there. I believe what is happening is selenium is still trying to search the element on the loading page.

I manually verified by monitoring the other page and it opens in under two second.

EDIT :

Html code present in the second page, i am searching by id :

<input type="text" id="crnum" name="number" title="Request Number" onfocus="this.className='focusField';" onblur="this.className='';" class="">
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
avinash
  • 29
  • 1
  • 7

1 Answers1

1

To get the screenshot of the Next Page (newly loaded page) you have to induce WebDriverWait for the identified element on the next page to be clickable and you can use the following code block :

password.send_keys(Keys.RETURN)
# Wait for the element with text as Request Number to be clickable
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='crnum' and @name='number' and @title='Request Number']")))
driver.save_screenshot('screenshot.png')
driver.quit()

Reference

You can find a detailed discussion in How to take screenshot with Selenium WebDriver

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I added the clickable line but I am still getting the error. For some reason, selenium is still searching the old page instead of the newly loaded page. – avinash May 15 '18 at 12:24
  • Delete the existing attachment first. Execute your program. Refresh your _Project Space_ and have a fresh look at the snapshot. – undetected Selenium May 15 '18 at 12:30
  • Hi, Debanjan did the above, the error still persists.I am able to take the screenshot by putting time.sleep(4) before taking the screenshot.Not sure what is happening. – avinash May 15 '18 at 13:09
  • Can you share the _url_ incase it's a public _url_? – undetected Selenium May 15 '18 at 13:10
  • It's internal company url, i wish i could share it as i really want to debug this. Can you explain why checking elements_to_be_clickable is better/more accurate than presense_of_elements_locatable? – avinash May 15 '18 at 14:25
  • @avinash Check this discussion [**`WebDriverWait not working as expected`**](https://stackoverflow.com/questions/49775502/webdriverwait-not-working-as-expected/49775808#49775808) – undetected Selenium May 15 '18 at 14:31