【问题标题】:two dynamic dropdown code for one is working fine other is not. at a time one is working in selenium java?一个的两个动态下拉代码工作正常,另一个不是。一次在 selenium java 中工作?
【发布时间】:2021-06-05 12:17:23
【问题描述】:
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);
        

【问题讨论】:

  • 您的描述(即标题)非常混乱。您能否尝试在问题正文中用整句话描述问题所在?在浏览器中手动使用时有什么作用?什么没有?什么在使用 Selenium 时有效,什么无效?
  • 我想说的是,如果我先评论,那么第二个就可以了
  • 好的,那么“不工作”是什么样的?不工作的会发生什么?它只是不做任何事情还是抛出任何异常?

标签: java selenium


【解决方案1】:

这里的问题是,当您从下拉列表中输入“源”并单击 Enter 时,目标选项卡会自动打开。

现在当您尝试点击以下代码时:

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

它不会运行,因为另一个元素在顶部

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

我尝试点击它并收到以下错误:

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)

更正的代码:根据当前情况

///目的地代码

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 2015-06-16
    • 2020-12-16
    • 1970-01-01
    • 2021-01-05
    • 2011-03-24
    相关资源
    最近更新 更多