首页 > 解决方案 > 如何在 Selenium 的酒店预订网站中选择自动弹出酒店名称

问题描述

我正在自动化以下酒店预订网站。在第一个搜索框中输入酒店后,我需要选择自动弹出的酒店名称......我不知道该怎么做。

我浏览了该链接并单击了演示,然后单击了页面上出现的第一个链接。我试图点击第一个搜索框,我需要从自动弹出列表中输入酒店......我不知道该怎么做,因为它没有 PAC 项目...... https://www.phptravels .net/home

public class Question1 {
    WebDriver Driver = null;
     WebDriverWait wait = null;
     String url = "https://phptravels.com/demo/";
    
     
     @BeforeTest
      public void beforeTest() {
         System.setProperty("webdriver.chrome.driver","src\\test\\resources\\drivers\\chromedriver.exe");
         ChromeOptions options = new ChromeOptions();
         options.setExperimentalOption("excludeSwitches",Arrays.asList("disable-popup-blocking"));
         options.addArguments("--disable-popup-blocking");
         
         Driver = new ChromeDriver(options);
         Driver.manage().window().maximize();
         Driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
         wait = new WebDriverWait(Driver, 25);

            
             String winHandle = Driver.getWindowHandle();
            //Driver.switchTo().window(winHandle);
             //new WebDriverWait(Driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[title='webpush-onsite']")));
             //new WebDriverWait(Driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button#deny.button.close"))).click();
      }
    
    @Test
  public void f() {
        Driver.get(url);
         System.out.println("*****In the main page*****");
        String xpathDemo = "//*[@id=\"mega-nav-navigation\"]/div/ul[1]/li[2]/a";
        Driver.findElement(By.xpath(xpathDemo)).click();
        String Title = "PHPTRAVELS | Travel Technology Partner";
        /*try {
        wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//*[@id=\"PopupSignupForm_0\"]/div[2]/div[1]")));
        Driver.findElement(By.xpath("//*[@id=\"PopupSignupForm_0\"]/div[2]/div[1]")).click();
        } catch(Exception e) {
            System.out.println("No popup..."+ e.getMessage());
        }
        */
        String username = Driver.findElement(By.xpath("//*[@id=\"Main\"]/section[2]/div/div/div[2]/div/div/div[2]/div[2]/div/div[3]/div[2]/div")).getAttribute("innerText");
        username = username.substring(6) ;
        String password = username.substring(30);
        System.out.println("Username text :"+username + "\npassword is:"+password);
        
        Driver.findElement(By.xpath("//*[@id=\"Main\"]/section[2]/div/div/div[2]/div/div/div[2]/div[2]/div/div[1]/div/a")).click();
        utils.HelperFunctions2.switchToWindow(Driver, Title);
        
        Driver.findElement(By.xpath("//*[@id=\"s2id_autogen16\"]")).click();
        Driver.findElement(By.xpath("/html/body/div[7]/ul")).click();
        
        
  }
  
  @AfterTest
  public void afterTest() {
      Driver.quit();
  }

}

标签: javaseleniumselenium-webdriverjunit4browser-automation

解决方案


xpath 下面是第一家酒店的结果,更改了它将与其余元素交互的索引。

将文本归档到酒店文本框后。给 Thread.sleep(2000); 在xpath下面使用。我希望它会工作

(.//ul[@class='select2-results']/following::div[@class='select2-result-label'])[2]

推荐阅读