How can I make Selenium / Python wait for a user to log in before continuing to execute it?

I am trying to run a script in Selenium / Python that requires logins at different points before the rest of the script can work. Is there a way to tell me the script to pause and wait on the login screen so that the user can manually enter the username and password (maybe something that is waiting for the page name to change before continuing with the script).

This is my code:

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() 
+6
source share
2 answers

Use WebDriverWait . For example, this does a Google search and then waits until an item is found 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 selenium.common.exceptions.TimeoutException if the lambda function continues to return False after 10 seconds.

You can find more information about WebDriverWait in the book Selenium .

+9
source
 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, when you want to run a script, you type loginUser() and it does its thing

this works because getpass.getpass() works exactly like input() , except that it does not show any characters (it is for accepting passwords and not showing it to anyone who looks at the screen)

So what happens, the page loads. then everything stops, your user manually logs in, and then returns to the python CLI and ends up in enter.

+3
source

Source: https://habr.com/ru/post/946539/


All Articles