java - 一个的两个动态下拉代码工作正常,另一个不是。一次在 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);
解决方案
这里的问题是,当您从下拉列表中输入“源”并单击 Enter 时,目标选项卡会自动打开。
现在,当您尝试点击以下代码时:
WebElement destination = driver.findElement(By.id("toCity"));
destination.sendKeys("Pak");
它不会运行,因为另一个元素在顶部
By.id("toCity"));
IEBy.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);
推荐阅读
- python - 如何在 tf.Estimator 检查点中保留不可训练的变量?
- fuzzing - 用于磁盘解析的 Libfuzzer 目标
- c - Pass a pointer to struct and a int to a function in C to implement a stack
- c# - 在启用自动生成主键的情况下,如何在 DbContext 的“OnModelCreating”方法期间播种主数据?
- python - Chrome 浏览器上的 Selenium Python 自动化
- laravel - 嗨,我现在如何使用下拉菜单 - LARAVEL 将数据(如广告)插入到这个嵌套集模型中?
- python - 尽管守护进程设置为 True,但 Python 线程并没有停止
- ios - Firebase Observer Event Type
- https - HTTPS 请求重启 NodeMCU
- android-studio - 实现 google playservice 时应用程序崩溃