Java 使用键解决自动提示下拉列表方法的任何方法
朗:爪哇 我一直在尝试自动化自动提示下拉列表,但如果我在单行中同时提供文本和键,则不会提供站点加载选项。记住这一点,我继续使用threadsleep方法并等待加载建议,但是光标从web元素中出来,并且没有执行键操作。请帮我做这个……Java 使用键解决自动提示下拉列表方法的任何方法,java,selenium,selenium-webdriver,selenium-chromedriver,selenium-ide,Java,Selenium,Selenium Webdriver,Selenium Chromedriver,Selenium Ide,朗:爪哇 我一直在尝试自动化自动提示下拉列表,但如果我在单行中同时提供文本和键,则不会提供站点加载选项。记住这一点,我继续使用threadsleep方法并等待加载建议,但是光标从web元素中出来,并且没有执行键操作。请帮我做这个…… import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebE
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 suggestivedropdown {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","C:\\Users\\teddy\\Downloads\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.makemytrip.com/");
// driver.findElement(By.xpath("//input[@id='fromCity']")).clear(); this is if it contains default option
WebElement source=driver.findElement(By.xpath("//input[@id='fromCity']"));
source.sendKeys("HYD");
Thread.sleep(3000);
source.sendKeys(Keys.ARROW_DOWN);
source.sendKeys(Keys.ENTER);
WebElement destination=driver.findElement(By.xpath("//input[@id='toCity']"));
destination.sendKeys("MUM");
Thread.sleep(3000);
destination.sendKeys(Keys.ARROW_DOWN);
destination.sendKeys(Keys.ENTER);
您不应该在代码中使用硬编码睡眠。如果您面临同步问题,请使用等待来避免问题,请参阅以下解决方案:
driver.get("https://www.makemytrip.com/");
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='fromCity']"))).sendKeys("Hyd");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//p[contains(text(),'Hyderabad, India')]"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='toCity']"))).sendKeys("Mumbai");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//p[contains(text(),'Mumbai, India')]"))).click();
感谢您提供的解决方案,但是否无法使用键方法从下拉列表中选择选项?这是一个自定义下拉列表,因此您无法直接处理它。您能告诉我您将如何处理Paytm flight下拉列表吗?它在应用程序中的何处?Paytm.com/flights