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="">