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

时间:2019-04-02 04:47:55

标签: selenium selenium-webdriver autosuggest

如何处理硒中该网站“ https://www.goibibo.com/”的“来自”和“目的地”框中的自动建议。 请帮助

我已经厌倦了使用基本方法,但是无法获得自动建议下拉菜单的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();
        }

    }

}

3 个答案:

答案 0 :(得分:2)

Chrome浏览器

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

enter image description here

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

enter image description here

Fire Fox Browser

第二个如何在Firefox中查找“自动填充”框的xpath-打开Firefox,然后右键单击并单击网站上的检查元素。有动画选项,因此它将打开所有DOM扩展,如下图所示。因此,通过阅读此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,否则   将引发异常。

答案 1 :(得分:0)

因此您可以尝试一种解决方案,请找到以下屏幕截图,

enter image description here

如您在屏幕快照中所见,如果我在文本框中键入M,则下拉列表将显示记录相对于字母'M'的情况;如果您在源代码中看到<ul>,则它是动态的,就像您在{{1 }},因此需要通过它的定位器来处理该下拉列表,因为它是动态的,因此首先需要在文本框中传递一些文本,然后需要使用<input>从硒中选择下拉列表中的元素{1}}或您可以使用的Select,您可以存储下拉列表中所有受尊重的来源(孟买,迈索尔等)并明智地使用

selectByVisibleText("")

我给你一个主意让我知道是否需要任何进一步的帮助

答案 2 :(得分:0)

使用下面的代码将起作用

masterBranch
相关问题