无法在下拉菜单中输入文本

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

我试着用下面的代码在一个自动提示的下拉菜单中输入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);
    }
}
selenium ui-automation
1个回答
0
投票

当你点击默认的 "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');

-1
投票

可能有很多原因导致它不能工作. 如果你能提供DOM元素就更好了。

然而,一个解决方案是通过JavaScript执行器输入文本,代码将是这样的:-。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.