英文:
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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论