[英]how to handle modal log-in using python selenium
I was trying to use python selenium to log in to a website.我试图使用 python selenium 登录网站。 But the login is in a bootstrap modal dialog box, I cannot log in successfully using my code.但是登录是在引导模式对话框中,我无法使用我的代码成功登录。 Could someone please let me know what is wrong with my code?有人可以让我知道我的代码有什么问题吗?
I have tried the following code:我尝试了以下代码:
import requests
import time
import os, sys
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import ActionChains
gecko = os.path.normpath(os.path.join(os.path.dirname(__file__), 'geckodriver'))
binary = FirefoxBinary(r'C:/Program Files/Mozilla Firefox/firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary, executable_path=gecko +'.exe')
driver.maximize_window()
wait = WebDriverWait(driver, 10)
time.sleep(1)
pageurl = "https://www.producemarketguide.com"
driver.get(pageurl)
popup = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="close-dialog"]')))
popup.click()
time.sleep(2)
login = wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[1]/header/div/div[2]/div[2]')))
login.click()
user = wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/app-main/app-widget/screen-layout/main/current-screen/div/screen-login/p[3]')))
user.send_keys('xxx@gmail.com')
password = wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/app-main/app-widget/screen-layout/main/current-screen/div/screen-login/p[4]/input')))
password.send_keys('XXXX')
submit = wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/app-main/app-widget/screen-layout/main/current-screen/div/screen-login/p[6]/button')))
submit.click()
The error message I got is我得到的错误信息是
Traceback (most recent call last):
File "pmg.py", line 34, in <module>
user = wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/app-main/app-widget/screen-layout/main/current-screen/div/screen-login/p[3]')))
File "C:\Program Files\Python38\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
That modal dialog is an iframe, so you need to switch to iframe to enter the credentials, try the below code:该模态对话框是 iframe,因此您需要切换到 iframe 以输入凭据,请尝试以下代码:
accept_cookies = driver.find_element(By.CSS_SELECTOR, "button#onetrust-accept-btn-handler")
if accept_cookies.is_displayed():
accept_cookies.click()
else:
print("Accept Cookies button not displayed")
popup_dialog = driver.find_element(By.ID,"popup-dialog-window")
close_icon = driver.find_element(By.ID, "close-dialog")
if popup_dialog.is_displayed():
close_icon.click()
else:
print("Welcome dialog not displayed")
time.sleep(1)
# Clicking Login/Register link
driver.find_element(By.CSS_SELECTOR, ".user-login.user-login-front.pull-right #user-piano-login-register").click()
time.sleep(1)
# Switch to the login dialog iframe
driver.switch_to.frame(driver.find_element(By.XPATH,".//iframe[starts-with(@id,'piano-id-')]"))
# enter the credentials and click Login button
driver.find_element(By.XPATH,".//input[@fieldloginemail]").send_keys("emailid")
time.sleep(0.5)
driver.find_element(By.XPATH,".//input[@fieldloginpassword]").send_keys("password")
time.sleep(0.5)
driver.find_element(By.XPATH,".//button[@actionlogin]").click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.