Java 无法在下拉列表中输入文本
我试着用下面的代码在一个自动提示下拉列表中输入BLR值,但是尽管它点击了它,它现在输入了文本Java 无法在下拉列表中输入文本,java,selenium,selenium-chromedriver,ui-automation,Java,Selenium,Selenium Chromedriver,Ui Automation,我试着用下面的代码在一个自动提示下拉列表中输入BLR值,但是尽管它点击了它,它现在输入了文本 import org.openqa.selenium.By; 导入org.openqa.selenium.Keys; 导入org.openqa.selenium.WebDriver; 导入org.openqa.selenium.WebElement; 导入org.openqa.selenium.chrome.ChromeDriver; 公共类测试用例2{ 公共静态void main(字符串[]args)
import org.openqa.selenium.By;
导入org.openqa.selenium.Keys;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.chrome.ChromeDriver;
公共类测试用例2{
公共静态void main(字符串[]args)引发InterruptedException{
//TODO自动生成的方法存根
System.setProperty(“webdriver.chrome.driver”,“//Users//suva//Downloads//chromedriver”);
WebDriver驱动程序=新的ChromeDriver();
驱动程序。获取(“https://www.makemytrip.com/");
WebElement source=driver.findElement(By.id(“fromCity”);
source.click();
System.out.println(source.isEnabled());
《睡眠》(2000年);
来源:发送密钥(“BLR”);
//source.sendKeys(键.箭头向下);
}
}
它不起作用的原因可能有很多。如果您还可以提供DOM元素,这将是有益的
不过,解决方案是通过JavaScript执行器输入文本。
代码将类似于:-
WebElement webelement = driver.FindElement(By.id("fromCity"));
JavaScriptExecutor executor = (JavaScriptExecutor)driver;
executor.ExecuteScript("arguments[0].value='" + "BLR" + "';", webelement);
为了提高上述代码的真实性,我需要DOM。但它应该可以工作。单击默认的“发件人”选项后,会出现一个下拉列表,其中包含另一个要键入的输入 试着这样做:
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');
你可以在driver.get中看到该网站,单击后会触发另一个输入。我会检查它。我没听到那句话。。谢谢