无法在下拉列表中输入文本

Unable to enter text in dropdown

我尝试使用以下代码在自动提示下拉菜单中输入值 BLR,但是尽管点击了它,但它现在正在输入文本。

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class testcase2 {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver", "//Users//suva//Downloads//chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.makemytrip.com/");
        WebElement source = driver.findElement(By.id("fromCity"));
        source.click();
        System.out.println(source.isEnabled());
        Thread.sleep(2000);
        source.sendKeys("BLR");
        //source.sendKeys(Keys.ARROW_DOWN);
    }
}

它不起作用的原因有很多。如果您也可以提供 DOM 元素,那将是有益的..

然而,解决方案是通过 JavaScript 执行器输入文本。 代码将类似于:-

WebElement webelement = driver.FindElement(By.id("fromCity"));
                JavaScriptExecutor executor = (JavaScriptExecutor)driver;
                executor.ExecuteScript("arguments[0].value='" + "BLR" + "';", webelement);

为了以上代码的真实性,我需要DOM。不过应该可以。

单击默认 'from' 选择后,会出现一个下拉菜单,其中包含另一个要输入的内容。

这样试试:

driver.get("https://www.makemytrip.com/");

WebElement triggerFromDropdown = driver.findElement(By.id("fromCity"));
triggerFromDropdown.click();

WebElement fromInput = driver.findElement(By.css(".autoSuggestPlugin input[placeholder='From']"));
fromInput.sendkeys('Dubai');