如何在Selenium中选择酒店预订网站上的自动弹出酒店名称

huangapple go评论84阅读模式
英文:

how to select the auto popup hotel names in a hotel booking site in 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("*****在主页*****");
		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 + "\n密码是:"+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();
	}
}
英文:

I am working on automating the following hotel booking site. I need to select the auto popup hotel name once I type the hotel in the first search box...I don't know how to do this.

I have navigated through the link and clicked Demo, then clicked the first link that appeared on the page.
I tried to click on the first search box and I need to enter a hotel from the auto popup list...I don't know how to do this because this has no PAC-item...
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();
}
}

答案1

得分: 1

以下xpath是第一个酒店的结果,更改索引将与其他元素进行交互。

在填写文本到酒店文本框之后。
给予Thread.sleep(2000);
使用以下xpath。我希望它会起作用

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

英文:

Below xpath is result of 1st hotel, changing the index it will intreact with rest of the elements.

After filing text to the hotel text box.
give Thread.sleep(2000);
use below xpath. I hope it will work

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

答案2

得分: 0

我已经构建了机器人、自动图像选择器等等,但我从未使用过 By.xpath 方法。使用 Classname 更容易!

public void f() {
    Driver.get(url);
    String getInputFocusId = "select2-input";
    Driver.findElement(By.className(getInputFocusId)).click();
    // 现在已经聚焦!
    String text = Driver.findElement(By.className("select2-match")).getText();
    // text 是列表中第一个默认的搜索匹配。
}

还有一种更复杂的方法。在 Selenium 中,您可以向元素发送键盘输入。

如果输入元素已经聚焦,第一个匹配项也将被聚焦。
通过点击 Enter 键,将搜索并显示聚焦的匹配项在输入元素中。
最后,您需要从输入元素中读取数据!

public void f() {
    Driver.get(url);
    String getInputFocusId = "select2-input";
    WebElement element = Driver.findElement(By.className(getInputFocusId));
    element.click();
    //element.sendKeys(Key.DOWN);
    element.sendKeys(Key.ENTER);
    String text = element.getText();
    // 这是第一个搜索匹配项的文本
    // 如果要获取第二个或第三个匹配项,只需重复发送 DOWN 键。
}

重要提示:确保每行都延迟执行(200毫秒是一个不错的时间)。这有助于您找到错误... 例如,在 Instagram 验证过程中,我延迟了很多行,最终成功了!

希望我的回答对您有帮助!

英文:

I have already built bots, auto image pickers and more and I have never used the By.xpath method. Classname is much easier to use!

public void f(){
Driver.get(url);
String getInputFocusId = "select2-input";
Driver.findElement(By.className(getInputFocusId).click();
//now focused!
String text = Driver.findElement(By.className("select2-match").getText();
//text is the first default search match of the list.
}

There is also a more complicated way.
In Selenium you can sendKeys to an Element.

If the input element is focused the first match is also focused.
By clicking on Enter the focused match will be searched and displayed in the input element.
Finally you have to read the data from the input Element!

public void f(){
Driver.get(url);
String getInputFocusId = "select2-input";
WebElement element = Driver.findElement(By.className(getInputFocusId);
element.click();
//element.sendKeys(Key.DOWN);
element.sendKeys(Key.ENTER);
String text = element.getText();
//this is the text of the first search match
//if you want to get the second or third just repeat sending the DOWN key.
}

IMPORTANT: Make sure to run each line delayed (200ms is a good time). This helps you finding errors... For example in the Instagram Auth Process I delayed a lot of lines, and it finally worked!

I hope my answer helps you!!!

huangapple
  • 本文由 发表于 2020年8月6日 22:16:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63285555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定