Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

two dynamic dropdown code for one is working fine other is not. at a time one is working in selenium java?

public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "E:\Selenium\Chrome Driver\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.makemytrip.com/");
        ///Code for source 
        Thread.sleep(5000);
        WebElement source=driver.findElement(By.id("fromCity"));
        source.sendKeys("mum");
        WebElement suggestion= driver.findElement(By.xpath("//input[@placeholder='From']"));
        suggestion.sendKeys(Keys.ARROW_DOWN);
        suggestion.sendKeys(Keys.ENTER);
        Thread.sleep(5000);
        
        ///Code for Destination

        WebElement destination = driver.findElement(By.id("toCity"));
        destination.sendKeys("Pak");
        WebElement suggestion2 = driver.findElement(By.xpath("//*[@placeholder='To']"));
        Thread.sleep(2000);
        suggestion2.sendKeys(Keys.ARROW_DOWN);
        suggestion2.sendKeys(Keys.ENTER);
        
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem here is that when you enter "source" from the dropdown and click on enter, the destination tabs get opened automatically.

enter image description here

Now when you try to hit the below code:

WebElement destination = driver.findElement(By.id("toCity"));
destination.sendKeys("Pak");

It wouldn't run because another element is on the top of

By.id("toCity")); i.e By.xpath("//*[@placeholder='To']")

enter image description here

I try to click on that and got the following error:

Exception in thread "main" org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <input data-cy="toCity" id="toCity" type="text" class="fsw_inputField lineHeight36 latoBlack font30" readonly="" value="Bangalore"> is not clickable at point (480, 255). Other element would receive the click: 


<input type="text" autocomplete="off" aria-autocomplete="list" aria-controls="react-autowhatever-1" class="react-autosuggest__input react-autosuggest__input--open react-autosuggest__input--focused" placeholder="To" value="">
  (Session info: chrome=91.0.4472.106)

Corrected Code: As per the current scenarios

///Code for Destination

        WebElement suggestion2 = driver.findElement(By.xpath("//*[@placeholder='To']"));
        suggestion2.sendKeys("Pak");
        Thread.sleep(2000);
        suggestion2.sendKeys(Keys.ARROW_DOWN);
        suggestion2.sendKeys(Keys.ENTER);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...