我可以导航到网站上的航班预订页面 - http://www.spicejet.com/。代码如下:
代码:
package Flight_Reservation;
import java.util.List;
import java.util.concurrent.TimeUnit;
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 NewFlight_OneWay {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver", "C:\\Selenium\\Selenium_Practice\\EXEs\\geckodriver-v0.10.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.spicejet.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[@id='ctl00_mainContent_rbtnl_Trip_1']")).click();
//Select origin
driver.findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXT")).sendKeys("DEL");
driver.findElement(By.linkText("Delhi (DEL)")).click();
//Select destination
driver.findElement(By.id("ctl00_mainContent_ddl_destinationStation1_CTXT")).sendKeys("BOM");
driver.findElement(By.linkText("Mumbai (BOM)")).click();
WebElement DateWidget = driver.findElement(By.id("ui-datepicker-div"));
List<WebElement> columns = DateWidget.findElements(By.tagName("td"));
for (WebElement cell: columns)
{
if (cell.getText().equals("24"))
{
cell.findElement(By.linkText("24")).click();
break;
}
}
Select AdultDropdown = new Select(driver.findElement(By.id("ctl00_mainContent_ddl_Adult")));
AdultDropdown.selectByValue("2");
Select ChildrenDropdown = new Select(driver.findElement(By.id("ctl00_mainContent_ddl_Child")));
ChildrenDropdown.selectByValue("1");
Select InfantDropdown = new Select(driver.findElement(By.id("ctl00_mainContent_ddl_Infant")));
InfantDropdown.selectByValue("1");
Select CurrencyDropdown = new Select(driver.findElement(By.id("ctl00_mainContent_DropDownListCurrency")));
CurrencyDropdown.selectByValue("INR");
driver.findElement(By.id("ctl00_mainContent_btn_FindFlights")).click();
}
}
问题:
在航班选择页面上,我想选择航班名称为“SG 161”的航班记录单选按钮。请让我知道如何实现这一目标?
答案 0 :(得分:1)
使用以下代码点击所需的单选按钮:
driver.findElement(By.xpath('//input[@type="radio"][contains(@value, "SG~ 161")]')).click();