动态下拉菜单在Selenium中的实现

huangapple go评论109阅读模式
英文:

dynamic dropdown in selenium

问题

这是我一直在尝试自动化 SpiceJet 航空公司的出发地和目的地字段的代码,但我遇到了一个错误,即无法找到元素,错误信息是 //a[@value='GOI'])[2]

有人可以帮助我吗?

此外,如果我继续尝试,就会显示错误,即无法定位起始字段的定位器,尽管之前它是有效的。有人可以解释一下吗?

编程语言:Java

driver.get("https://www.spicejet.com");
driver.findElement(By.xpath("//input[@id='ctl00_mainContent_ddl_originStation1_CTXT']")).click();
driver.findElement(By.xpath("//a[@text='Amritsar (ATQ)']")).click();
driver.findElement(By.xpath("(//a[@value='GOI'])[2]")).click();
英文:

This is the code with the help of which i have been trying to automate spicejet from and to fields, the error i am getting is unable to find element i.e,, //a[@value='GOI'])[2].

can someone please help me?

Also if i keep on trying then it shows the error that the very from field locator is unable to locate, though it worked earlier. can someone please explain this to me?

Lang:java

driver.get("https://www.spicejet.com");
driver.findElement(By.xpath("//input[@id='ctl00_mainContent_ddl_originStation1_CTXT']")).click();    
driver.findElement(By.xpath("//a[@text='Amritsar (ATQ)']")).click();
driver.findElement(By.xpath("(//a[@value='GOI'])[2]")).click();

答案1

得分: 0

你的 xpath 完全没问题。你需要使用一些 WebDriverWait() 并等待 elementToBeClickable()

driver.get("https://www.spicejet.com")
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='ctl00_mainContent_ddl_originStation1_CTXT']"))).click()
new WebDriverWait(driver, 5).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='Amritsar (ATQ)']"))).click()
new WebDriverWait(driver, 5).until(ExpectedConditions.elementToBeClickable(By.xpath("(//a[@value='GOI'])[2]"))).click()
英文:

Your xpath is absolutely fine.You need to induce some WebDriverWait() and wait for elementToBeClickable().

driver.get("https://www.spicejet.com");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='ctl00_mainContent_ddl_originStation1_CTXT']"))).click(); 
new WebDriverWait(driver, 5).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@text='Amritsar (ATQ)']"))).click();
new WebDriverWait(driver, 5).until(ExpectedConditions.elementToBeClickable(By.xpath("(//a[@value='GOI'])[2]"))).click();

huangapple
  • 本文由 发表于 2020年4月7日 23:05:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/61083190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定