I'm trying to run a script in Selenium/Python that requires logins at different points before the rest of the script can run. Is there any way for me to tell the script to pause and wait at the login screen for the user to manually enter a username and password (maybe something that waits for the page title to change before continuing the script).
This is my code so far:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import unittest, time, re, getpass
driver = webdriver.Firefox()
driver.get("https://www.facebook.com/")
someVariable = getpass.getpass("Press Enter after You are done logging in")
driver.find_element_by_xpath('//*[@id="profile_pic_welcome_688052538"]').click()
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. This means ,you cannot use this to wait for human interactions. But you can assume that the user is waiting for some link to appear and call click().
Selenium: Waiting Until the Element Is Visiblevar wait = new WebDriverWait(driver, TimeSpan. FromSeconds(20)); As you can see, we give the WebDriverWait object two parameters: the driver itself and a TimeSpan object that represents the timeout for trying to locate the element.
We can make Selenium wait for 10 seconds. This can be done by using the Thread. sleep method. Here, the wait time (10 seconds) is passed as a parameter to the method.
Use WebDriverWait. For example, this performs a google search and then waits for a certain element to be present before printing the result:
import contextlib
import selenium.webdriver as webdriver
import selenium.webdriver.support.ui as ui
with contextlib.closing(webdriver.Firefox()) as driver:
driver.get('http://www.google.com')
wait = ui.WebDriverWait(driver, 10) # timeout after 10 seconds
inputElement = driver.find_element_by_name('q')
inputElement.send_keys('python')
inputElement.submit()
results = wait.until(lambda driver: driver.find_elements_by_class_name('g'))
for result in results:
print(result.text)
print('-'*80)
wait.until
will either return the result of the lambda function, or a selenium.common.exceptions.TimeoutException
if the lambda function continues to return a Falsey value after 10 seconds.
You can find a little more information on WebDriverWait
in the Selenium book.
from selenium import webdriver
import getpass # < -- IMPORT THIS
def loginUser():
# Open your browser, and point it to the login page
someVariable = getpass.getpass("Press Enter after You are done logging in") #< THIS IS THE SECOND PART
#Here is where you put the rest of the code you want to execute
THEN whenever you want to run the script, you type loginUser()
and it does its thing
this works because getpass.getpass()
works exactly like input()
, except it doesnt show any characthers ( its for accepting passwords and notshowing it to everyone looking at the screen)
So what happens is your page loads up. then everything stops, Your user manually logs in, and then goes back to the python CLI and hits enter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With