Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...