java - 如何处理selenium中的自动建议

标签 java selenium selenium-webdriver

//for source
 WebElement from= driver.findElement(By.id("autocomplete_source"));
      from.clear();
      from.sendKeys(FromCity);
      Thread.sleep(3000);
      Actions builder=new Actions(driver);
      builder.moveToElement( from.findElement(By.xpath("//*[@class='acResults']/ul/li[1]/span"))).click().build().perform();

//for destination

 WebElement to=driver.findElement(By.id("autocomplete_dest"));
      to.clear();
      driver.findElement(By.id("autocomplete_dest")).sendKeys(ToCity);
builder.sendKeys(Keys.ARROW_DOWN).click().build().perform();
      WebDriverWait wait=new WebDriverWait(driver, 90);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*        [@class='acResults']/ul/li/span")));
     to=driver.findElement(By.cssSelector(".acResults>ul>li>span"));
      Thread.sleep(2000);
          to.click();

/* 有两个文本框源和目标,源正在处理,但目标不起作用。 当我在目的地列表中经过金奈时,显示向下箭头正在工作,但它没有选择并且给出错误定位器不可见 */

最佳答案

提供 HTML。 我在 redbus.in 上尝试了自动建议,并且无需使用 Action 类即可工作。 这是一个适用于我在 FF/Chrome 上的代码 -

driver.get("http://www.redbus.in/");
driver.findElement(By.xpath(".//*[@id='txtSource']")).sendKeys("pune");
driver.findElement(By.xpath(".//*[@id='txtDestination']")).sendKeys("Chennai");

请提供 url 或 html,以便我们深入研究。

下面的代码适用于我的 FF( http://in.via.com/bus-tickets)-

driver.get("http://in.via.com/bus-tickets/");
WebElement from = driver.findElement(By.xpath(".//*[@id='autocomplete_source']"));
WebElement to =driver.findElement(By.xpath(".//*[@id='autocomplete_dest']"));
from.clear();
from.sendKeys("Pune");
to.clear();
to.sendKeys("Chennai");
WebDriverWait wait=new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("html/body/div[9]/ul/li")));
driver.findElement(By.xpath("html/body/div[9]/ul/li")).click();

//点击提交

driver.findElement(By.xpath(".//*[@id='bookingDiv']/div[21]/input")).click();

关于java - 如何处理selenium中的自动建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23863425/

相关文章:

javascript - setAttribute ('display' ,'block' )不工作 selenium-webdriver Node js

eclipse - 指向 eclipse IDE 以使用 JAVA_HOME 中指定的 JRE/JDK 之外的另一个 JRE/JDK

java - Applet:如何将参数从Applet传递到html

python - WebDriverException : Message: Service/content/chromedriver unexpectedly exited. 状态代码为:-6,使用 ChromeDriver Google Colab 和 Selenium

testing - 使用具有随机值的 Selenium IDE

c# - 获取 IE 屏幕截图返回黑色图像

java - Headless Chrome 在页面上查找输入字段

java - Java中基类构造函数调用重写方法时派生类对象的状态

java - 如何使用 smack 库将自定义 IQ 节发送给另一个用户?

java - 如何迭代每个循环以获取重复值