I'm trying to write Login credentials using send_keys element method with Selenium on this alert:
Login Alert
I get this alert after clicking on a button in a new window but I can't inspect the elements on this window.
here is my code
import time
import pynput
from selenium import webdriver
driver=webdriver.Chrome("C:/Users/dhias/OneDrive/Bureau/stgg/chromedriver.exe")
driver.get("http://10.15.44.177/control/userimage.html")
time.sleep(3)
window_before = driver.window_handles[0]
driver.maximize_window()
btn=driver.find_element_by_name("TheFormButton3")
btn.click()
window_after = driver.window_handles[1]
driver.switch_to.window(window_after)
obj = driver.switch_to.alert
time.sleep(2)
obj.send_keys('Admin')
time.sleep(2)
I get this error that tells me that there is no alert
I can't acess to the page so I can't try my code but, first I optimized It by adding WebdriverWait and the new By. function:
from selenium import webdriver as wd
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = wd.Chrome("C:/Users/dhias/OneDrive/Bureau/stgg/chromedriver.exe")
wait = WebDriverWait(driver, 15)
driver.get("http://10.15.44.177/control/userimage.html")
driver.maximize_window()
window_before = driver.window_handles[0]
wait.until(EC.element_to_be_clickable((By.NAME, "TheFormButton3"))).click()
driver.switch_to.window(driver.window_handles[1])
alert = driver.switch_to.alert
alert.send_keys('Admin\npassword\n') # \n works as an ENTER key
You can try this code to see if the alert really exists or is an element of the webpage:
from selenium.common.exceptions import NoSuchElementException
def is_alert_present(self):
try:
self.driver.switch_to_alert().send_keys('Admin\npassword\n') # \n works as an ENTER key
print('Sucess')
except NoSuchElementException:
print('No Alert Present')
Also see Alerts in the Selenium Documentation
Also Try:
parent_h = browser.current_window_handle
# click on the link that opens a new window
handles = browser.window_handles # before the pop-up window closes
handles.remove(parent_h)
browser.switch_to_window(handles.pop())
# do stuff in the popup
# popup window closes
browser.switch_to_window(parent_h)
# and you're back
So finally I've found a solution..I used the approach where you have to send username and password in URL Request and it worked:
http://username:password#the-site.com
Related
I am working on a project, wherein I am trying to automate the recaptchas. It has been going good so far, however I have run into an issue. Here is the code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
from selenium import *
from selenium.webdriver import *
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
import requests
def initialize():
options = Options()
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
wait = WebDriverWait(driver, 20)
return driver, wait
def input_info():
name = driver.find_element(By.XPATH, "/html/body/div[3]/div/div/div[1]/form/div[1]/input")
name.send_keys("usernamer12392103821")
password = driver.find_element(By.XPATH, "/html/body/div[3]/div/div/div[1]/form/div[2]/input")
password.send_keys("Password123")
verify_password = driver.find_element(By.XPATH, "/html/body/div[3]/div/div/div[1]/form/div[3]/input")
verify_password.send_keys("Password123")
def select_recaptcha():
men_menu = wait.until(ec.visibility_of_element_located((By.XPATH, "/html/body/div[3]/div/div/div[1]/form/div[7]/div/div/div/iframe")))
men_menu.click()
def get_to_sound():
print("Searching for sound button")
time.sleep(3)
sound_butt_iframe = driver.find_element(By.XPATH, "/html/body/div[5]/div[4]/iframe")
print("Step one complete")
driver.switch_to.frame(sound_butt_iframe)
print("Step two complete")
sound_butt = sound_butt_iframe.find_element(By.XPATH, "/html/body/div/div/div[3]/div[2]/div[1]/div[1]/div[2]/button")
print("Step 4 complete")
driver.switch_to.default_content()
print("Step 5 complete")
sound_butt.click()
### set up driver to go to reddit page!
driver, wait = initialize()
driver.get('https://old.reddit.com/login')
time.sleep(5)
#### Fill in the information necessary! (username, password)
input_info()
time.sleep(3)
print("Finding recaptcha")
select_recaptcha()
print("looking to see if successful!")
try:
successful = wait.until(ec.visibility_of_element_located((By.XPATH, "/html/body/div[5]")))
print("Failed")
print("Call to API with sound file!")
try:
get_to_sound()
except:
print("Failed")
except:
print("Succeeded")
print("Carry on")
time.sleep(10000)
Basically, it will open the https://old.reddit.com/login webpage, enter in a dummy username and password (twice for the confirm) and then click on the recaptcha. This all works great. It realizes that the recaptcha "pops" up, and then it tries to click on the audio button, which is where I run into the problem. I think because it is "stuck" in a new iframe html tag, the XPATH isn't being registered, but I'm not sure. Does someone know how I can get the driver to press the audio control button? I've attached a screenshot below of the html stuff that would be needed for this.
It's the highlighted button tag at the bottom that I am "worried" about
Thanks in advance.
Edit: Here's the picture of the button I am trying to press. I took a picture of the whole recaptcha thing, and the button I am trying to press is the headphones at the bottom.
you should get iframe as element with driver.find_element(By.XPATH), and then find_element(By.XPATH internal) against that element
for that use switch_to_frame method
iframe = driver.find_element_by_xpath("//iframe[#title='...']")
driver.switch_to_frame(iframe)
# here access to internal elements by xpath
driver.find_element(By.XPATH, "")
driver.switch_to.default_content()
If NoSuchElementException accrued and xpath is right then the scroll to target button :
driver.execute_script('arguments[0].scrollIntoView(true);',
driver.find_element_by_xpath(XPATH OF Target button))
I need to accept cookies on a specific website but I keep getting the NoSuchElementException. This is the code for entering the website:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
chrome_options = Options()
driver = webdriver.Chrome(executable_path='./chromedriver', options=chrome_options)
page_url = 'https://www.boerse.de/historische-kurse/Erdgaspreis/XD0002745517'
driver.get(page_url)
time.sleep(10)
I tried accepting the cookie button using the following:
driver.find_element_by_class_name('message-component message-button no-children focusable button global-font sp_choice_type_11 last-focusable-el').click()
driver.find_element_by_xpath('//*[#id="notice"]').click()
driver.find_element_by_xpath('/html/body/div/div[2]/div[4]/div/button').click()
I got the xpaths from copying the xpath and the full xpath from the element while using google chrome.
I am a beginner when it comes to selenium, just wanted to use it for a short workaround. Would appreciate some help.
The button Zustimmen is in iframe so first you'd have to switch to the respective iframe and then you can interact with that button.
Code:
driver.maximize_window()
page_url = 'https://www.boerse.de/historische-kurse/Erdgaspreis/XD0002745517'
driver.get(page_url)
wait = WebDriverWait(driver, 30)
try:
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[starts-with(#id,'sp_message_iframe')]")))
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Zustimmen']"))).click()
print('Clicked successfully')
except:
print('Could not click')
pass
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I've got a similar problem with a page. I've tried to address the iframe and the "accept all" button with selenium (as suggested by #cruisebandey above).
However, the pop-up on this page seems to work differently:
https://www.kreiszeitung-wochenblatt.de/hanstedt/c-panorama/mega-faslams-umzug-in-hanstedt_a270327
This is what I've tried:
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path="C:\\Users\\***\\chromedriver.exe")
driver.maximize_window()
try:
driver.get("https://www.kreiszeitung-wochenblatt.de/hanstedt/c-panorama/mega-faslams-umzug-in-hanstedt_a270327")
except:
print('Site not found')
wait = WebDriverWait(driver,10)
try:
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,'/html/body/iframe')))
except:
print('paywall-layover not found')
try:
cookie = wait.until(EC.element_to_be_clickable((By.XPATH,'//*[#id="consentDialog"]/div[3]/div/div[2]/div/div[2]/div/div[1]/div[2]/div')))
cookie.click()
except:
print('Button to accept all not found')
I am currently working on a project where I am automating whatsapp messages through url. like this https://wa.me/number_here
whenever i do it normally everything goes fine, but when I try to automate this a whatsapp popup box appears and blocks everything, I mean everything, no right-click no developer options, that is why i cant get the x path of the button on that popup. i have tried (driver.shift_to,alert.close)but it says that there is no such alert. i have tried to find the button by contains(text) method but also did not work. here is my code.
chrome_options = Options()
chrome_options.add_argument('--user-data-dir = user-data')
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
chrome_options.add_argument('--disable-notifications')
chrome_options.add_argument('--disable-popup-blocking')
chrome_options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=chrome_options, executable_path= driver_path)
wait = WebDriverWait(driver, 60)
contact = f'https://wa.me/{number}'
driver.get(contact)
time.sleep(3)
alert = wait.until(EC.alert_is_present())
alert.accept()
please help me how to bypass this popup. thanks
I think, this link can be helpful for you:
https://stackoverflow.com/a/19019311/12000849
What I do is to set a conditional delay with WebDriverWait just before the point I expect to see the alert, then switch to it, like this:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
browser = webdriver.Firefox()
browser.get("url")
browser.find_element_by_id("add_button").click()
try:
WebDriverWait(browser, 3).until(EC.alert_is_present(),
'Timed out waiting for PA creation ' +
'confirmation popup to appear.')
alert = browser.switch_to.alert
alert.accept()
print("alert accepted")
except TimeoutException:
print("no alert")
0
I am trying to do a tutorial and learn Selenium in python however i cant seem to get Selenium to enter the password into the password field/form box. It enters the email addres perfectly fine, and inside the code the password is typed correctly however the website returns it as "Incorrect password entered". However when i type the password in manually it logs in as it is correct. I have removed the email and password for security
I am using:
Python v3.9
Chrome v87
This is the URL i am practicing on:
https://www.aria.co.uk/myAria/ShoppingBasket?action=checkout
And this is my current code:
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
from selenium.webdriver.common.action_chains import ActionChains
import time
# Open Chromedriver
driver = webdriver.Chrome(r"C:\Users\Ste1337\Desktop\chromedriver\chromedriver.exe")
# Open webpage
driver.get("https://www.aria.co.uk/SuperSpecials/Other+products/ASUS+ROG+Pugio+2+Wireless+Optical+RGB+Gaming+Mouse?productId=72427")
#https://www.aria.co.uk/Products/Components/Graphics+Cards/NVIDIA+GeForce/GeForce+RTX+3060+Ti/Palit+GeForce+RTX+3060+Ti+Dual+8GB+GPU?productId=73054
# Click "Add to Basket" or refresh page if out of stock
try:
element = WebDriverWait(driver, 1).until(EC.presence_of_element_located((By.XPATH, "Out of Stock!")))
time.sleep(5)
browser.refresh()
except:
button = driver.find_element_by_id("addQuantityButton")
button.click()
basket = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID, "basketContent")))
basket.click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//img[contains(#src,'/static/images/checkoutv2.png')]"))).click()
login = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME,"login[email]")))
login.send_keys("testuser#hotmail.co.uk")
login.send_keys(Keys.TAB)
driver.implicitly_wait(2)
login.send_keys("password")
driver.implicitly_wait(2)
login.send_keys(Keys.ENTER)
You haven't defined somewhere to put the password. Currently, the password in MEANT to be entered in the same place as the email. To change that, define where the password is meant to go. This is how I did it:
passwrd = driver.find_element_by_xpath("/html/body/div[4]/div[1]/div[2]/form/div/h1/div[3]/div/div/input[2]")
passwrd.send_keys("password")
So in total, the code should look like this:
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
from selenium.webdriver.common.action_chains import ActionChains
import time
# Open Chromedriver
driver = webdriver.Chrome(r"C:\Users\eesam\AppData\Local\Programs\Python\Python39\Scripts\chromedriver.exe")
# Open webpage
driver.get("https://www.aria.co.uk/SuperSpecials/Other+products/ASUS+ROG+Pugio+2+Wireless+Optical+RGB+Gaming+Mouse?productId=72427")
#https://www.aria.co.uk/Products/Components/Graphics+Cards/NVIDIA+GeForce/GeForce+RTX+3060+Ti/Palit+GeForce+RTX+3060+Ti+Dual+8GB+GPU?productId=73054
# Click "Add to Basket" or refresh page if out of stock
try:
element = WebDriverWait(driver, 1).until(EC.presence_of_element_located((By.XPATH, "Out of Stock!")))
time.sleep(5)
browser.refresh()
except:
button = driver.find_element_by_id("addQuantityButton")
button.click()
basket = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID, "basketContent")))
basket.click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//img[contains(#src,'/static/images/checkoutv2.png')]"))).click()
login = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME,"login[email]")))
login.send_keys("testuser#hotmail.co.uk")
login.send_keys(Keys.TAB)
driver.implicitly_wait(2)
passwrd = driver.find_element_by_xpath("/html/body/div[4]/div[1]/div[2]/form/div/h1/div[3]/div/div/input[2]")
passwrd.send_keys("password")
driver.implicitly_wait(2)
login.send_keys(Keys.ENTER)
I'm trying to interact with a page that requires entry of a username and a passcode, then requires a click on a reCAPTCHA and then a click on a login button. Entry of the username and passcode and clicking on the reCAPTCHA work fine, but after interacting with the reCAPTCHA, the login button element seems to be unfindable by Selenium. The thing is, the login button element is easily found by Selenium before interacting reCAPTCHA. What's going on?
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
driver = webdriver.Firefox()
driver.get("https://localbitcoins.com/accounts/login/")
time.sleep(2)
element = driver.find_element_by_name("username")
element.send_keys("username")
element = driver.find_element_by_name("password")
element.send_keys("passcode")
time.sleep(1)
# reCAPTCHA
wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_xpath('//iframe[contains(#src, "google.com/recaptcha")]')))
wait(driver, 10).until(EC.element_to_be_clickable((By.ID, "recaptcha-anchor"))).click()
# login button
elements = driver.find_elements_by_xpath("//button[contains(text(), 'Login')]")
elements[0].click()
To be able to handle "Login" button you have to switch back from iframe after interacting with it:
# reCAPTCHA
wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_xpath('//iframe[contains(#src, "google.com/recaptcha")]')))
wait(driver, 10).until(EC.element_to_be_clickable((By.ID, "recaptcha-anchor"))).click()
# Switch to default content
driver.switch_to.default_content()
# login button
elements = driver.find_elements_by_xpath("//button[contains(text(), 'Login')]")
elements[0].click()
P.S. Note that there is no need to use time.sleep(1) before wait(driver, 10). If you need more time to wait - just increase timeout, e.g. wait(driver, 15)