【问题标题】:how handle auto suggest in "from" and "destination" box for this website "https://www.goibibo.com/" in selenium如何在 selenium 中为该网站“https://www.goibibo.com/”处理“来自”和“目的地”框中的自动建议
【发布时间】:2019-08-23 07:53:11
【问题描述】:

如何在 selenium 中为该网站“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();
        }

    }

}

【问题讨论】:

标签: selenium selenium-webdriver autosuggest


【解决方案1】:

Chrome 浏览器

首先如何在 Chrome 浏览器中查找自动填充框的 XPATH 打开您的网站,然后单击检查元素并立即单击源选项卡,单击以打开您的自动填充框并按 **F8** **Key for pause debugger**。然后单击您的元素选项卡,您可以轻松获取您的 xpath,请参阅下面的 snap 以获取更多信息。所以它会冻结你的HTML。

现在单击元素并创建您自己的 xpath。

火狐浏览器

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

不是如何从自动填充框中查找元素。请参考下面的代码 sn-p。

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 否则它 会抛出异常。

【讨论】:

  • 但我的错是什么?
  • @akshaypatil 你没有使用 break 加上 Xpath 不正确。
  • 是的,因此我没有在我的回答中给出看看为什么我们需要 break 我已经使用了 foreach 所以......
  • @akshaypatil 如果我删除中断;它将在线程“main” org.openqa.selenium.StaleElementReferenceException 中抛出异常:过时的元素引用:元素未附加到页面文档
  • 但它选择了元素然后也是它抛出异常的原因
【解决方案2】:

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

正如您在屏幕截图中看到的那样,如果我在文本框中键入 M,则下拉菜单会显示与字母“M”相关的记录,如果您在源代码中看到 &lt;ul&gt;,它是动态的,正如您在 &lt;input&gt; 下方看到的那样,所以您需要通过它的定位器处理该下拉列表,它是动态的,因此首先您需要在文本框中传递一些文本,然后您需要在 selenium 中使用 Select 从下拉列表中选择元素,您使用 selectByVisibleText("") 或其他您可以使用List&lt;Element&gt; 您可以存储来自下拉列表的所有受人尊敬的来源(孟买、迈索尔等)并明智地使用它

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();
}

我给了你一个想法,如果你需要任何进一步的帮助,请告诉我

【讨论】:

  • 请检查我的代码一次。它总是选择一些随机位置
【解决方案3】:

我已经通过 selenium 和 python 实现了自动化。它在列表中收集所有建议的城市,然后单击所需的城市。

from selenium import webdriver

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.goibibo.com/")
driver.implicitly_wait(3)
listCity = []
driver.find_element_by_xpath("//input[@id='gosuggest_inputSrc']").send_keys("JA")
cities = driver.find_elements_by_xpath("//div[@class='mainTxt clearfix']//preceding-sibling::span")
for city in cities:
    listCity.append(city.text)

for city in cities:
    if "Jagdalpur" in city.text:
        city.click()
        break

print(listCity)
print(len(listCity))

【讨论】:

    【解决方案4】:

    使用下面的代码就可以了

    Webelement ele=driver.findelement()
    
    Actions ob = new Actions(driver);
    ob.moveToElement(ele);
    ob.click(ele);
    Action action  = ob.build();
    action.perform();
    

    【讨论】:

    • 其实我不知道如何处理这个。我是硒新手
    • Webelement ele=driver.findelementbyxpath("Paste ur xpath over here") Actions ob = new Actions(driver); ob.moveToElement(ele); ob.click(ele);动作动作 = ob.build();动作.执行();它会工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 2021-10-08
    • 1970-01-01
    • 2015-09-18
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    相关资源
    最近更新 更多