【问题标题】:I cannot choose from drop down menu via Selenium Webdriver我无法通过 Selenium Webdriver 从下拉菜单中进行选择
【发布时间】:2017-06-27 15:01:14
【问题描述】:

我使用 Selenium + Eclipse。我需要从下拉菜单中选择项目,但是我有一个问题,可能找不到该元素。我的代码如下:

package firstTC;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.chrome.ChromeDriver;;

public class Testcase {


    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\nazar\\Desktop\\New folder\\chromedriver.exe");
        ChromeDriver driver = new ChromeDriver(); 
        driver.get("https://www.goindigo.in/");
        driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
        Select oSelect = new Select(driver.findElement(By.xpath(".//*[@id='roundWay']/form/div[1]/ul[1]/li[1]/input[1]")));
        oSelect.selectByIndex(3);

    }

}

我尝试了不同的定位器,但没有帮助。

【问题讨论】:

  • maybe element cannot be found... 也许吧?您想让我们猜测您的代码有什么问题吗?
  • 请参考How to Ask。更具体地说,请描述发生了什么,您是否收到错误?如果是这样,它是什么?另外,请提供相关的html。谢谢!

标签: eclipse selenium selenium-webdriver automation automated-tests


【解决方案1】:

您要查找的不是“下拉菜单”。首先尝试点击它:

driver.findElement(By.xpath(".//*[@id='roundWay']/form/div[1]/ul[1]/li[1]/input[1]")).click();

然后点击子菜单:

driver.findElement(By.xpath(".//li[@data-val='IXA']")).click();

【讨论】:

    【解决方案2】:

    我去过 GoIndigo 网站并尝试检查您在代码中提到的 XPath。 您的代码中提到的 XPath 用于“发件人”字段(如果我错了,请纠正我),它是一个 文本框(不是 下拉列表

    所以,而不是

    Select oSelect = new Select(driver.findElement(By.xpath(".//*[@id='roundWay']/form/div[1]/ul[1]/li[1]/input[1]")));
            oSelect.selectByIndex(3);
    

    应该使用

    driver.findElement(By.xpath(".//*[@id='roundWay']/form/div[1]/ul[1]/li[1]/input[1]").sendKeys("something");
    

    【讨论】:

      【解决方案3】:

      试试下面的代码:

      driver.get("https://www.goindigo.in/");
      driver.manage().window().maximize();
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      driver.findElement(By.xpath("//input[@placeholder='From']")).click();
      WebDriverWait wait = new WebDriverWait(driver, 15);
      wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//li[@data-val='BLR']"))));
      driver.findElement(By.xpath("//li[@data-val='BLR']")).click();
      

      解释:首先使用xpath定位器点击From元素,然后等到你想要点击的元素。对于wait,我使用Explicit wait 方法。

      在这里,在我的示例中,我想点击Bengaluru,因此基于Bengaluru,我为Bengaluru city 创建了一个xpath。

      如果您想点击其他城市而不是 Bengaluru,那么您必须为该城市编写正确的 xpath。

      【讨论】:

        【解决方案4】:

        您尝试使用Select 类的方式,它不是那种下拉菜单。

        您的价值观来自<li> 标签。用户使用以下代码来选择您的值 -

        // Click on the "From" textbox  
        
        driver.findElement(By.xpath("//div[@id='roundWay']//li/input[@placeholder='From']")).click();   
        
        // Select the required from city
        
        driver.findElement(By.xpath("//ul[@class='city-name origin-city-name']//li[text()='Bagdogra (IXB)']")).click();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-15
          • 2023-04-02
          • 1970-01-01
          • 2015-01-18
          • 2019-02-25
          相关资源
          最近更新 更多