如何使用java选择并单击selenium web驱动程序中的单选按钮

时间:2017-08-22 08:48:26

标签: java selenium selenium-webdriver webdriver selenium-firefoxdriver

我在java中编写了一个简单的selenium web驱动程序脚本来选择并点击一个单选按钮,但我无法选择并点击网页上的第二个单选按钮'ONE WAY FLIGHT':{{1} }

这是我的代码:

http://www.lot.com/pl/en

这就是错误:

  

WebDriver driver = new FirefoxDriver(); String web = "http://www.lot.com/pl/en"; driver.get(web); driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); WebElement element; element = driver.findElement(By.id("select2-departureAirport-container")); element.click(); element.sendKeys("WAW"); element.sendKeys(Keys.ENTER); /* RADIO BUTTON ERROR */ element = driver.findElement(By.cssSelector("input[value='SINGLE']")); elementRadio.click();

这是网页Exception in thread "main" org.openqa.selenium.ElementNotInteractableException:上的HTML片段:

http://www.lot.com/pl/en

我无法理解它为什么会抛出异常。

2 个答案:

答案 0 :(得分:0)

使用以下代码: -

WebDriver driver = new FirefoxDriver();
String web = "http://www.lot.com/pl/en";
driver.get(web);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
WebElement element;

element = driver.findElement(By.id("select2-departureAirport-container"));
element.click();
element.sendKeys("WAW");
element.sendKeys(Keys.ENTER);

/* RADIO BUTTON ERROR */
element = driver.findElement(By.cssSelector("input[value='SINGLE']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

答案 1 :(得分:0)

您尝试使用xpath吗?

elementRadio = driver.findElement(By.xpath("//*[@id="flightBookingForm"]/div[2]/div[1]/div/fieldset/label[2]/span[2]"));
elementRadio.click();