任何解决使用按键的自动建议下拉方法的方法。

huangapple go评论83阅读模式
英文:

Any method to solve auto-suggestive dropdown method using keys

问题

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);
    }
}
英文:

lang:java

I have been trying to automate auto suggestive dropdowns, but if I give the both the text and keys in single line, it is not giving the site to load options. Keeping this thing in mind, I went ahead with thread sleep method and waited for the suggestions to load, but the cursor is coming out of the web element and keys operations are not being performed. please help me with this....

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);

答案1

得分: 1

你不应在代码中使用硬编码的延迟。如果遇到同步问题,请使用等待操作来避免问题,参考以下解决方案:

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();
英文:

You shouldn't use hard coded sleeps in the code . If you are facing synchronization issue use waits to avoid issues, Refer below solution :

    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();

huangapple
  • 本文由 发表于 2020年4月10日 00:20:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/61125654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定