Java 从两个具有相同类名的下拉列表的第一个中选择一个选项
我已经选择了最低票价并点击了查看座位,我正试图从第一个下拉菜单(登机点)中选择一个选项,但无法选择,因为它们都有相同的类名。谢谢你的帮助 我使用的代码是:Java 从两个具有相同类名的下拉列表的第一个中选择一个选项,java,selenium,xpath,drop-down-menu,Java,Selenium,Xpath,Drop Down Menu,我已经选择了最低票价并点击了查看座位,我正试图从第一个下拉菜单(登机点)中选择一个选项,但无法选择,因为它们都有相同的类名。谢谢你的帮助 我使用的代码是: package week3.redbus; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import j
package week3.redbus;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class BookATicket {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.redbus.in/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
//page1
driver.findElement(By.id("txtSource")).sendKeys("Hyderabad");
driver.findElement(By.id("txtDestination")).sendKeys("Chennai");
driver.findElement(By.id("txtOnwardCalendar")).click();
driver.findElement(By.xpath("//div[@id = 'rbcal_txtOnwardCalendar']/table[1]/tbody/tr[4]/td[3]")).click();
driver.findElement(By.xpath("//div/button[@id = 'searchBtn']")).click();
List <WebElement> fares = driver.findElements(By.xpath("//div/span[@class = 'fareSpan']//span"));
int fareNum = fares.size();
int[] fareInt = new int [fareNum];
int index =0;
System.out.println( "Num of fares is "+fareNum );
for (WebElement e : fares) {
String fareN = e.getText();
fareInt[index] = Integer.parseInt(fareN);//convert string to integer array
index++;
}
for (int i : fareInt) {
System.out.println(i);
}
Arrays.sort(fareInt);//sort to ascending order
System.out.println("Sorted fare list");
for (int i : fareInt) {
System.out.println(i);
}
//the least price
System.out.println("Lowest price is "+fareInt[0]);
String x = Integer.toString(fareInt[0]);
driver.findElement(By.xpath("//span[text() = '"+ x +"']/../../../button[@class = 'viewSeatsBtn']")).click();
//if (driver.findElement(By.xpath("//select[@class ='select-apsrtc']/option[text() = '-- Boarding points --']")).isDisplayed()) {
//driver.findElement(By.xpath("//div[@class = 'selectContainer BPContainer']/select")).click();
Thread.sleep(3000);
Select bp = new Select(driver.findElement(By.xpath("//div[@class = 'selectContainer BPContainer']/select[@class = 'select-apsrtc']")));
bp.selectByIndex(6);
driver.findElement(By.xpath("//button[@class = 'continueBtn']")).click();
//}
}
}
但是,我得到了以下例外:
Exception in thread "main" org.openqa.selenium.WebDriverException: Element is not clickable at point (471.5, 105.80000305175781). Other element would receive the click: <div class="SortBar clearfix MB"></div>
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Cannot locate element with text: Ameerpet - 05:25 PM
使用而不是
findElement()
。这将返回一个列表
,而不是一个Web元素
,您可以选择列表中的第一个或第二个项目。您可以使用以下定位器(css选择器)唯一标识这两个下拉列表。
登机点下拉列表的定位器为:
[class='selectContainer BPContainer'] [class='select-apsrtc']
[class='selectContainer DPContainer'] [class='select-apsrtc']
用于放置点下拉列表的定位器为:
[class='selectContainer BPContainer'] [class='select-apsrtc']
[class='selectContainer DPContainer'] [class='select-apsrtc']
如果需要,也可以使用xPath。您已经有了一个正确的xPath。
对于滴点使用:
//div[@class = 'selectContainer DPContainer']/select[@class = 'select-apsrtc']
然后,您可以执行以下操作:
Select bp = new Select(driver.findElement(By.xpath("//div[@class = 'selectContainer BPContainer']/select[@class = 'select-apsrtc']")));
bp.selectByIndex(6);
Select droppingPoint = new Select(driver.findElement(By.xpath("//div[@class = 'selectContainer DPContainer']/select[@class = 'select-apsrtc']")));
droppingPoint.selectByIndex(1);
如果您对此有任何疑问,请告诉我。如果我使用findElements(),我会遇到以下异常:线程“main”java.lang.ClassCastException中的异常:java.util.ArrayList无法转换为org.openqa.selenium.WebElement请不要尝试在注释中放置堆栈跟踪。使用附加信息编辑原始问题,以便其他可能的回答者更容易看到。是的,
findElements()
返回一个列表,与我说的完全一样。您已经有一个位置正在使用findElements()
(查看票价
),所以这应该不是问题。我不能或不能将findElements()与Select一起使用。在这里,我只能给出findElement()。如果您不能更具体一些,我真的无法帮助您。你有什么问题?您尚未说明为什么不能使用findElement()
。感谢您的回复。正如您前面所说,我知道如何通过xpath选择这两个选项,但是,我只需要选择第一个dropbox选项,因为第二个选项已经被选中。当我使用它时,我得到了一个例外,这是我在我的问题中发布的。你能告诉我哪里出错了吗?是的,当然可以,你能试着替换代码行driver.findElement(By.xpath(“//button[@class='continueBtn']”)。单击()使用driver.findElement(By.xpath(“//button[@class='show seatLayout'])进行代码>单击()代码>。我复制并运行了您的代码,但在最后一行出现错误。在最后一步中单击按钮之前,还添加了一些等待试用的内容。您发布的有问题的错误显示“元素不可单击”。表示您在单击元素时出错,而不是在选择下拉选项时出错。我在您阅读时正在编辑。对不起:)我现在就贴了。