java - 使用键解决自动建议下拉方法的任何方法
问题描述
语言:java
我一直在尝试自动化自动提示下拉菜单,但如果我在单行中同时提供文本和键,则不会让网站加载选项。牢记这一点,我继续使用线程睡眠方法并等待加载建议,但是光标从 Web 元素中出来并且没有执行键操作。请在这件事上给予我帮助....
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);
解决方案
您不应该在代码中使用硬编码的 sleeps。如果您遇到同步问题,请使用等待来避免问题,请参阅以下解决方案:
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();
推荐阅读
- stm32 - 使用 STM32 DAC 进行频率调整
- wordpress - 无法通过 FTP 删除 AWS EC2 上的文件,权限被拒绝
- matlab - 如何在 MATLAB 中生成包含这样的矩阵的图像?
- java - 如何配置从 SQS 获取消息并将它们移动到 Kafka 主题的 Kafka 连接器?
- json - 如何使用 JOLT 将数组元素和字符串转换为数组中的后续单独对象?
- reactjs - 创建一个中间步骤以使用 firebase 验证电子邮件地址,反应原生
- php - 如何使用记事本++在php中数组变量的开头和结尾添加单引号
- java - Azure 和 Java 字符编码
- python - 我无法在 Django 中的应用程序 URL 之间连接
- javascript - elasticsearch按get请求中的字段名称排序不起作用