如何在硒中的“来自”和“目的地”框中自动建议该网站“https://www.goibibo.com/”

问题描述 投票:0回答:3

如何处理自动建议在“从”和“目的地”框中为本网站“https://www.goibibo.com/”在selenium。请帮忙

我厌倦了使用基本方法但无法获得自动建议的X路径下拉

无法点击下拉列表

   package basic;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class goibibo {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.goibibo.com/");

        new WebDriverWait(driver, 20)
                .until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='gosuggest_inputSrc']")))
                .sendKeys("Mum");
        List<WebElement> myList = new WebDriverWait(driver, 20).until(
                ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//*[@id=\"react-autosuggest-1\"]")));
        for (WebElement element : myList) {
            if (element.getText().contains("Mumbai"))
                ;
            element.click();
        }

    }

}
selenium selenium-webdriver autosuggest
3个回答
2
投票

Chrome浏览器

首先,如何在Chrome浏览器中查找自动填充框的XPATH打开您的网站,而不是单击Inspect元素并立即单击源选项卡,单击以打开自动填充框,然后按F8键暂停调试器。然后单击“元素”选项卡,您可以轻松获取下面的xpath参考以获取更多信息。所以它会冻结你的HTML。

enter image description here

现在单击Elements an创建您自己的xpath。

enter image description here

Fire Fox浏览器

其次如何在Firefox中找到自动填充框的xpath - 打开Firefox并右键单击并单击网站上的检查元素。有动画选项,所以它会打开你所有的DOM Expanded,如下图所示。所以通过阅读这个dom结构,您可以轻松创建XPATH。

enter image description here

不是如何从自动填充框中找到元素。请参阅下面的代码段。

package com.software.testing;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Testingclass extends DriverFactory {

    private static WebDriver driver = null;

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", "your driver path");
        driver = new ChromeDriver();
        driver.get("https://www.goibibo.com/");
        new WebDriverWait(driver, 20)
                .until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='gosuggest_inputSrc']")))
                .sendKeys("A");
        Thread.sleep(1000);
        List<WebElement> myList = new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfAllElementsLocatedBy(
                By.xpath("//div[@class='dib marginL10 pad0 textOverflow width90']/div/span")));
        for (int i = 0; i < myList.size(); i++) {
            System.out.println(myList.get(i).getText());
            if (myList.get(i).getText().equals("Ahmedabad")) {
                myList.get(i).click();
                break;
            }
        }

    }
}

在条件语句之后不要忘记使用break,否则会抛出异常。


0
投票

所以你可以尝试一个解决方案,请找到下面的截图,

enter image description here

正如您在屏幕截图中看到的那样,如果我在文本框中键入M,则下拉列表显示对字母'M'的记录,如果您在源代码中看到<ul>是动态的,就像您在<input>下方看到的那样,您需要通过它来处理该下拉列表定位器它是动态的,因此首先你需要在文本框中传递一些文本,然后你需要使用Select中的selectByVisibleText("")从下拉列表中选择元素,或者你可以使用List<Element>你可以存储所有受尊重的来源(孟买,迈索尔等)来自下拉并明智地使用它

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='gosuggest_inputSrc']))).sendKeys("M");
List<WebElement> myList = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("Xpath of the dynamic drop down")));
for (WebElement element:myList) {
         if(element.getText().contains("Mumbai"));
         element.click();
    }

我给了你一个想法让我知道你是否需要任何进一步的帮助


0
投票

使用下面的代码它将起作用

Webelement ele=driver.findelement()

Actions ob = new Actions(driver);
ob.moveToElement(ele);
ob.click(ele);
Action action  = ob.build();
action.perform();
© www.soinside.com 2019 - 2024. All rights reserved.