gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:20:29 26 4
gpt4 key购买 nike

我用java编写了一个简单的selenium Web驱动程序脚本来选择并单击单选按钮,但我无法选择并单击网页上的第二个单选按钮“单向飞行”: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:

<div class="b-row">
<div class="b-column twelve">
<fieldset class="flight-type" role="radiogroup" aria-required="true">
<legend class="acc-hide">Choose flight type</legend>
<label class="booker-label radio-label">
<input data-f-focus="radio" id="ticketTypeReturn" type="radio" name="ticketType" class="required" value="RETURN" checked="checked" />

<span class="ci" aria-hidden="true"></span>
<span class="ci-label">Round-trip flight</span>
</label>
<label class="booker-label radio-label g-no-margin">

<input data-f-focus="radio" type="radio" name="ticketType" class="required" value="SINGLE" />
<span class="ci cis" aria-hidden="true"></span>
<span class="ci-label">One-way flight</span>
</label>
<p id="ticketTypeReturn--required" class="b-v-error" role="alert">Choose flight type</p>
</fieldset>

</div>
</div>

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

最佳答案

使用下面的代码:-

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);

关于java - 如何使用 java 选择并单击 selenium Web 驱动程序中的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45812979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com