Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need Selenium Help with Popup
#1
I'm trying to write a script (Python 2) to log in to a web site. However, in doing so there is a popup that occurs (allow cookies) preventing me from filling in the login. I have the following function to do the login but it does not catch the popup.

.
.
.
from selenium import webdriver
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
.
.
.
options=FirefoxOptions()
options.add_argument("--headless")
driver=webdriver.Firefox(executable_path="/usr/local/bin/geckodriver",options=options)
.
.
.

def reSend(address):
        uri="https://app.mailjet.com/signin"
        driver.get(uri)
        wait=WebDriverWait(driver,3)
        el=wait.until(EC.presence_of_element_located((By.ID,'onetrust-accept-btn-handler')))
        driver.find_element_by_id('onetrust-accept-btn-handler').click()
        print(driver.page_source)
        login=driver.find_element_by_id('login')
        login.send_keys(USER_NAME)
        passwd=driver.find_element_by_id('password')
        passwd.send_keys(PASSWORD)
        submit=driver.find_element_by_id('form-btn').click()
         .
         .
         .
.
.
.
The popup has no title or form name but I know the button ID. However, the code I am using either does not wait for the popup or cannot find that ID. It is also possible that the popup will not occur after the first login but I know I have not reached that point yet. If you use that link you can see what I am dealing with. Can someone spot what I am doing wrong? TIA.
Reply
#2
Can be done like this,no need switch window just find Allow cookie button.
Then a new problem as has to solve CAPTCHA before can login,can do manually in browser as it open when not using headless.
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
import time

# Setup
# https://storage.googleapis.com/chrome-for-testing-public/125.0.6422.141/win64/chromedriver-win64.zip
options = Options()
#options.add_argument("--headless=new")
options.add_argument("--lang=en")
ser = Service(r"C:\cmder\bin\chromedriver.exe")
browser = webdriver.Chrome(service=ser, options=options)
# Parse or automation
url = "https://app.mailjet.com/signin"
browser.get(url)
time.sleep(3)
cookie_bt = browser.find_element(By.CSS_SELECTOR, '#onetrust-accept-btn-handler')
cookie_bt.click()
email = browser.find_element(By.CSS_SELECTOR, '#login')
email.send_keys('[email protected]')
password = browser.find_element(By.CSS_SELECTOR, '#password')
password.send_keys('123456')
time.sleep(3)
log_in = browser.find_element(By.CSS_SELECTOR, '#form-btn')
log_in.click()
Reply
#3
Thanks. I'll give that a try.
Reply
#4
That doesn't seem to work but I have more testing to do. In the mean time does geckodriver use the same cookie and profile location as the browser itself? I can log in from the browser without the cookie popup and captcha. Also how do I detect a captcha request (I know I can't answer it but I want to output a message and quit the script.
Reply
#5
(Jul-08-2024, 08:16 PM)gw1500se Wrote: In the mean time does geckodriver use the same cookie and profile location as the browser itself? I can log in from the browser without the cookie popup and captcha
When use selenium it's a new browser session every time,that's why cookie and captcha will show up every time.
When use your browser session cookie is saved and no need to use cookie and captcha every time.
There is a way to save cookies in selenium.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Selenium click on popup button??? GuJu 7 10,859 Jul-20-2019, 09:21 AM
Last Post: Nizam
  Issue closing an Modal Alert (popup) leviathan54 8 7,499 Apr-23-2019, 02:00 PM
Last Post: leviathan54
  Generic If Popup Exists Close It Script digitalmatic7 1 2,886 Feb-18-2018, 07:24 AM
Last Post: metulburr
  popup login form in django uditvashisht 0 4,198 Jan-31-2018, 11:55 PM
Last Post: uditvashisht
  Error in Selenium: CRITICAL:root:Selenium module is not installed...Exiting program. AcszE 1 4,288 Nov-03-2017, 08:41 PM
Last Post: metulburr
  selenium bypass javascript popup box metulburr 6 9,497 Jun-02-2017, 07:15 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020