使用Python Selenium无法选中登录弹窗的问题排查与解决
问题:Selenium无法交互Test.dk登录弹窗的解决办法?
我尝试使用Python和Selenium在页面https://www.test.dk/完成登录操作,但登录弹窗始终无法正常交互。打开弹窗后,Python无法识别它,无法点击其中任何元素。我排查后发现它既不是window handle、alert window也不是iframe,请问这可能是什么原因?该如何操作这个弹窗?
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 from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.chrome.options import Options options = Options() options.add_argument('--disable-dev-shm-usage') # 初始化浏览器驱动 driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options) # 打开目标网站 driver.get("https://www.test.dk/") # 等待并点击Cookie授权按钮 accept_banner = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.CLASS_NAME, "coi-banner__accept")) ) accept_banner.click() # 等待并点击"登录"按钮 login_button = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Log ind']")) ) login_button.click() # 等待"使用邮箱登录"按钮加载完成 email_login_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//button[contains(text(), "Log ind med email")]'))) # 点击"使用邮箱登录"按钮 email_login_button.click() driver.quit()
补充说明:
问题并非出在弹窗本身,而是页面存在两个包含用户名、密码输入框及提交按钮的表单。以下是调整后的代码:
# 定位所有包含"使用邮箱登录"文本的元素 email_login_buttons = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, '//span[contains(text(), "Log ind med email")]'))) # 选择第二个"使用邮箱登录"按钮 email_login_button = email_login_buttons[1] # 执行点击操作 email_login_button.click()
内容的提问来源于stack exchange,提问作者Whit3H0rse
相关产品推荐
相关产品推荐

