You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Java Selenium的Airbnb多弹窗处理:登录密码弹窗交互问题

问题描述

我已通过谷歌账号完成Airbnb网站的注册,当前网站弹出要求输入密码的登录弹窗,但无法与其进行交互。此前我已成功处理了谷歌注册弹窗,现请求协助解决该密码登录弹窗的交互问题,相关代码如下:

package Practice;
import org.junit.Assert;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.Alert;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.ArrayList;
import java.time.Duration;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;


public class Airbnbmumbai
{
    public static void main(String[]args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "path to chromedriver");
        ChromeOptions ops = new ChromeOptions();
        ops.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(ops);
        driver.get("https://www.airbnb.co.in/");
        driver.manage().window().maximize();
        driver.navigate().forward();
        Thread.sleep(1000);
        // Clicking on trending //
        driver.findElement(By.xpath("//div[@class=\"f19g2zq0 dir dir-ltr\"]")).click();
        driver.navigate().forward();
        Thread.sleep(2000);
        // Getting the title of the page on starting of the page //
        System.out.println("Page title is : " + driver.getTitle());
        driver.findElement(By.xpath("//input[@name=\"query\"]")).sendKeys("Mumbai");
        Thread.sleep(2000);
        driver.findElement(By.xpath("//div[@class=\"_r1t6ga\"]")).click();
        driver.findElement(By.xpath("(//div[@class=\"_1gazwvry notranslate\"])[4]")).click();
        driver.navigate().forward();
        driver.findElement(By.xpath("/html/body/div[5]/div/div/div[1]/div/div[2]/div/div/div/div/div/div[1]/div/div/div/header/div/div[2]/div[2]/div/div/div/form/div[2]/div/div[3]/div[4]/div/div/section/div/div/div/div/div[2]/div[1]/div/div[1]/div/div/div/div/div[2]/div[2]/div/div[3]/div/table/tbody/tr[5]/td[4]/div")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath("//div[@class=\"p1kudodg dir dir-ltr\"]")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath("(//span[@class=\"i98ho2o dir dir-ltr\"])[2]")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath("/html/body/div[5]/div/div/div[1]/div/div[2]/div/div/div/div/div/div[1]/div/div/div/header/div/div[2]/div[2]/div/div/div/form/div[2]/div/div[5]/div[2]/div/div/div/section/div/div/div/div[2]/div[2]/div/button[2]/span")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath("//span[@class=\"t1l18xbv dir dir-ltr\"]")).click();
        driver.navigate().forward();
        Thread.sleep(2000);
        driver.findElement(By.xpath("/html/body/div[5]/div/div/div[1]/div/div[2]/div/div/div/div/div/div[2]/main/div[2]/div/div[2]/div/div/div/div/div[1]/div/div/div/div/div[2]")).click();
        System.out.println("Page title is : " + driver.getTitle());
        Thread.sleep(2000);
        driver.findElement(By.xpath("/html/body/div[5]/div/div/div[1]/div/div[2]/div/div/div/div/div/div[2]/main/div[2]/div/div[2]/div/div/div/div/div[7]/div/div[2]/div/div/div/div[1]")).click();
        // Now will work on the second window //
        ArrayList<String> tabs2 = new ArrayList<String>(driver.getWindowHandles());
        driver.switchTo().window(tabs2.get(1));
            // Clicking on see all photos //
        System.out.println("Page title is : " + driver.getTitle());
        Thread.sleep(4000);
        driver.navigate().forward();
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollBy(0,450)", "");
        // Clicking on reserve //
     driver.findElement(By.xpath("(//span[@class=\"t12u7nq4 dir dir-ltr\"])[2]")).click();
     Thread.sleep(4000);
     driver.navigate().forward();
        js.executeScript("window.scrollBy(0,450)", "");
        // Signup with google //
        driver.findElement(By.xpath("(//div[@class=\"_1u21f56\"])[2]")).click();
        String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
        String subWindowHandler = null;
        Set<String> handles = driver.getWindowHandles(); // get all window handles
        Iterator<String> iterator = handles.iterator();
        while (iterator.hasNext()){
            subWindowHandler = iterator.next();
        }
        driver.switchTo().window(subWindowHandler); // switch to popup window

        // Now you are in the popup window, perform necessary actions here
        Thread.sleep(2000);
        driver.navigate().forward();
        Thread.sleep(2000);
        driver.findElement(By.xpath("//*[@id=\"identifierId\"]")).sendKeys("username");
        Thread.sleep(2000);
        driver.navigate().forward();
        driver.findElement(By.xpath("//*[@id=\"identifierNext\"]/div/button/span")).click();
        Thread.sleep(4000);
        driver.navigate().forward();
        driver.findElement(By.xpath("//*[@id=\"password\"]/div[1]/div/div[1]/input")).sendKeys("Password");
        Thread.sleep(2000);
        driver.navigate().forward();
        driver.findElement(By.xpath("//*[@id=\"passwordNext\"]/div/button/span")).click();
        // Since the user is already registered so entering password here //
        System.out.println("Page title is : " + driver.getTitle());
        // Handling the pop up again //
        driver.manage().timeouts().implicitlyWait(4,TimeUnit.SECONDS);
        driver.navigate().forward();
        
        driver.findElement(By.name("password")).sendKeys("Mypassword");

        }
    }
解决方案

针对无法与Airbnb密码登录弹窗交互的问题,可按以下步骤修复:

  • 切回正确窗口:完成谷歌账号登录后,谷歌弹窗会关闭,需切回Airbnb的主窗口,代码中已保存parentWindowHandler,直接调用即可:
// 切回Airbnb主窗口
driver.switchTo().window(parentWindowHandler);
  • 用显式等待替代Thread.sleep:硬编码等待时间不稳定,用显式等待确保密码输入框加载完成且可交互:
// 创建10秒超时的等待实例
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
// 等待密码输入框可点击
WebElement passwordInput = wait.until(ExpectedConditions.elementToBeClickable(By.name("password")));
passwordInput.sendKeys("Mypassword");
  • 检查元素定位准确性:如果By.name("password")无法定位到元素,通过浏览器开发者工具查看弹窗中密码输入框的实际属性,改用更精准的定位方式,比如:
WebElement passwordInput = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(@class, 'login-modal')]//input[@type='password']")));
  • 清理无效代码:代码中多次调用driver.navigate().forward()没有实际作用,还会干扰页面状态,建议全部移除。

内容的提问来源于stack exchange,提问作者Bug Hunter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 06:27:02