I'm trying to automate data entry using selnium in chorme but after each click on the website I'm getting a sign in pop up similar to the image with is attached below. Is Selenim is capable to handle such alerts?
Sign In Alert
I have tried following python code
try:
WebDriverWait(browser, 10).until(EC.alert_is_present())
alert = browser.switch_to_alert()
alert.dismiss()
except TimeoutException:
pass
What's the right way to handle these kinds of pop up for browser automation?
This is a Basic Authorization login prompt. Unfortunately, you can't automate it with Selenium. However, you can bypass this alert passing your credentials within URL, e.g:
username = ... # your username here
password = ... # password here
driver.get(f"https://{username}:{password}#us-east-023.staticnetconent.com")
as I can understand if you click ESC each time the pop up appears it closes, so why not automate the ESC pressing action too, let's try this :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
WebDriverWait(browser, 10).until(EC.alert_is_present())
# the following simulates the ESC button pressing
webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform()
Try this:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver=webdriver.Firefox()
driver.get('//Url need to be pass')
time.sleep(3)
driver.switch_to.alert.send_keys(username + Keys.TAB + password)
time.sleep(3)
driver.switch_to.alert.accept()
Related
I used the following code to login to Instagram with Selenium:
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.instagram.com/")
time.sleep(1)
username = driver.find_element_by_xpath('//*[#id="loginForm"]/div/div[1]/div/label/input')
username.send_keys('username')
password = driver.find_element_by_xpath('//*[#id="loginForm"]/div/div[2]/div/label/input')
password.send_keys('password')
time.sleep(1)
loginbutton = driver.find_element_by_xpath('//*[#id="loginForm"]/div/div[3]/button/div')
loginbutton.click()
time.sleep(7)
notnow = driver.find_element_by_class_name("yWX7d")
notnow.click()
time.sleep(3)
Everything works but Instagram asks for save login using a pop-up. How do I press the "Not now" button in the pop-up?. I have tried using xpath without success.
Try to send Esc to the page or to this pop up.
from selenium.webdriver.common.keys import Keys
webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform()
or
driver.find_element_by_xpath('pop_up_xpath').send_keys(Keys.ESCAPE)
Try this:
driver.find_element_by_xpath("//button[contains(text(), 'Not Now')]").click()
I am completely new to Python and Selenium and writing an automated login scipt to check the availability of the appointment. I wrote the belwo code to login. The issue is whenever I use the breakpoint and manually step-in the code everything runs fine. But when I run it without breakpoint it cathes the exception at "Button click failed". I also tried adding sleep timing but nothing helped. All worked with stepping-in but without the breakpoints. Any help will be appreciated :)
Here is the code I wrote:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
try:
Login_Button = driver.find_element_by_xpath("//input[#name='Login'][#type='button']")
Login_Button.click()
time.sleep(5)
except:
print("Button click failed")
This is the button code, which I am trying to click
The exception is thrown at Line Login_button.click() with the following message
Message: element click intercepted: Element is not clickable at point (510, 497). Other element would receive the click: ...
Most login forms, accept an alternate way of sending the data by pressing 'Enter' after sending the credentials, if you can't find the correct sign-in button using the Xpath either (see: How to locate elements), then you can try replicating the Enter key with:
from selenium.webdriver.common.keys import Keys
try:
password.send_keys(Keys.ENTER)
time.sleep(5)
except:
print("Submitting login form failed")
Hello and happy Holidays!
I wrote a script where I open a webpage in chrome, login, go to a certain page and then trying to print that page.
When the Chrome print preview comes up, I can not target the print button neither try to just hit enter via automation. Nothing works.
Any ideas please?
Ps: This is a the code I wrote but the login information will be incorrect
Thank you in advance!
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from shutil import which
import time
from pynput.keyboard import Key, Controller
keyboard = Controller()
chrome_path = which("chromedriver")
driver = webdriver.Chrome(executable_path="chromedriver")
name = '****'
password = '****'
driver.get(
"https://www.winbank.gr/sites/corporate/el/Pages/default.aspx")
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
username = driver.find_element_by_name('Username')
username.send_keys(name)
passwords = driver.find_element_by_name('Password')
passwords.send_keys(password)
passwords.send_keys(Keys.ENTER)
time.sleep(10)
driver.find_element_by_xpath(
'//*[#id="ng-app"]/body/div[3]/div/div/div/div/div/div/div/div[2]/div[2]/div[1]/div[2]/div/div[2]/div/div/div[6]').click()
window_before = driver.window_handles[0]
time.sleep(6)
driver.find_element_by_xpath(
'//*[#id="ng-app"]/body/div[3]/div/div/div/div/div/div/div/div[3]/div[2]/wb-pfm-transactions/div/div[2]/div/div[22]/div[2]').click()
time.sleep(3)
driver.switch_to.window(driver.window_handles[-1])
driver.find_element_by_id('print').click()
driver.switch_to.window(driver.window_handles[-1])
print('-------------test----------')
# driver.print = "function(){};"
keyboard.press(Key.enter)
driver.find_element_by_xpath(
'/html/body/print-preview-app//print-preview-sidebar//print-preview-button-strip//cr-button[2]').click()
Usually when dealing with untargetable objects I use the tab key to navigate and enter to "click" the element currently focused, what you should do is:
1. Try to print it manually, but with using tabs to navigate and Enter to click
2. Keep track of all key presses needed
3. Write your code accordingly
Edit: Also, if you need help with automatically pressing keys please refer to this link.
I've a Login form like below.
Textbox for username, password, captcha and SIGN-IN button are clickable and visible from beginning.
Using Selenium I can provide input for username & password.
Then, I've to wait for entering the CAPTCHA by users, and then click on SIGN IN button by user again.
After clicking the SIGN-IN button webdriver should take the control for next.
So, webdriver should wait until the SIGN-IN button is clicked (for user1, it may take 2 seconds to enter CAPTCHA, but for user2 it may take 5 seconds to enter the CAPTCHA).
This is the HTML for SIGN IN button.
<button _ngcontent-c4="" class="search_btn" type="submit">SIGN IN</button>
I tried with below, But, it's not working.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("url")
btnSignIn = driver.find_element_by_xpath("//form/button[#type='submit' and #class='search_btn']")
WebDriverWait(driver, timeout=600).until(EC.staleness_of(btnSignIn))
How can I do that ?
Thanks in advance.
You can implement below solution:
driver = webdriver.Chrome()
driver.get("url")
# Code for entering Username, Password
...
# Wait until user enter Captcha
input("Press ENTER after filling CAPTCHA")
driver.find_element_by_xpath("//form/button[#type='submit' and #class='search_btn']").click()
This should allow to wait until user press ENTER key and then execute Submit button click
If you don't want user to interact with browser, but with console only, you can improve code as below:
driver = webdriver.Chrome()
driver.get("url")
# Code for entering Username, Password
...
# Wait until user enter Captcha (in console) and press ENTER
captcha_input = driver.find_element_by_xpath('//input[#placeholder="Enter Captcha"]')
captcha = input("Enter CAPTCHA and Press ENTER\n")
captcha_input.send_keys(captcha)
driver.find_element_by_xpath("//form/button[#type='submit' and #class='search_btn']").click()
Similar to Andersson's answer, which didn't work for me because the driver complained it couldn't find the element (my guess is the driver is stateful so after logging in the driver didn't have the new page's data)
driver = webdriver.Chrome()
driver.get("url")
# Code for entering Username, Password
...
# Wait until user enter Captcha (in console) and press ENTER
input("Login manually and press ENTER\n")
driver.get("logged in url") # cookies should keep you logged in
driver.find_element_by_xpath("blah")
But I found what worked for me is adding a line to reload the url. This will reload the site but thanks to cookie the driver is now logged in. Hope this helped if his previous answer didn't :)
You may try this,
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(btnSignIn))
I am getting very frustrated trying to login to costar.com with python and selenium. I have tried it on the chrome browser and firefox browser, but can't figure out the correct code.
I have logged into other websites, but cannot figure out how to input text into the login boxes for this site.
Here's what I have so far:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Chrome(executable_path="C:/Users/Gus Gabel/Anaconda/chromedriver.exe")
browser.get("http://www.costar.com")
browser.maximize_window()
#the username and password boxes are hidden until you press the login button on the home page
login = browser.find_element_by_id("loginLink")
login.click()
#now that the username and password boxes are available, I've tried finding the elements by class_name, xpath, (BY.NAME), id, etc and nothing has worked
#here are a few codes that haven't worked and the errors associated
user = browser.find_element_by_id('username')
NoSuchElementException: Message: no such element
user = browser.find_element_by_class_name('usernameNew')
NoSuchElementException: Message: no such element
#when i try to use the above code with "elements" instead of "element", no error message pops up.
user = browser.find_elements_by_class_name('usernameNew')
#but then whey i try to choose which element by doing this
user = browser.find_element_by_class_name('usernameNew')[0]
NoSuchElementException: Message: no such element
How is it possible that there can be a list of elements, but yet not have an initial element?
If anyone can figure out how to input text into the username and password text boxes of costar.com, I will be greatly appreciative. I can't figure this out for the life of me!
To enter text into an input box with Selenium (e.g, your user name into the username field), use the send_keys method of the element:
input_element.send_keys('some_text_string')
You can also try Selenium's action_chains module, as in the following code (untested), to get past the roadblock from #dm295's answer:
from selenium import webdriver
from selenium.webdriver.common import action_chains
driver = webdriver.Firefox()
driver.maximize_window()
driver.get("http://www.costar.com")
action = action_chains.ActionChains(driver)
login = driver.find_element_by_class_name("login-icon")
login.click()
driver.switch_to.frame(driver.find_element_by_id('custlogin'))
username = driver.find_element_by_id('username')
action.move_to_element(username).perform()
username.send_keys('Gus Gabel')
A couple of things:
(a) you have to wait until the page is loaded (or at least the part of the page that you are interested in)
(b) only maximized browser window works for me (depends on device/resolution)
(c) you are trying to click the wrong element
import time
from selenium import webdriver
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.maximize_window()
driver.get("http://www.costar.com")
try:
# wait 15 seconds till the login link is present
login = WebDriverWait(driver, 15).until(
EC.presence_of_element_located((By.CLASS_NAME, "login-icon"))
)
login.click()
# just sleep 5 seconds to show that the login link was clicked
time.sleep(5)
# do other stuff (probably fill in username and password) ...
finally:
driver.quit()
Read the documentation regards waiting in selenium.
EDIT
Manged to get one step further (switch to iframe), but the following code raises
selenium.common.exceptions.ElementNotVisibleException: Message:
Element is not currently visible and so may not be interacted with
from selenium import webdriver
driver = webdriver.Firefox()
driver.maximize_window()
driver.get("http://www.costar.com")
try:
login = driver.find_element_by_class_name("login-icon")
login.click()
# switch to the custlogin iframe
driver.switch_to.frame(driver.find_element_by_id('custlogin'))
username = driver.find_element_by_id('username')
# this will raise:
# selenium.common.exceptions.ElementNotVisibleException: Message:
# Element is not currently visible and so may not be interacted with
username.send_keys('username')
finally:
driver.quit()