【问题标题】:Radio Button selection using Javascript executor使用 Javascript 执行器选择单选按钮
【发布时间】:2016-08-29 20:08:27
【问题描述】:

我正在尝试在页面中选择“是”“否”选项,因为正常的“Click()”不起作用我为此使用了 JSExecutor!问题是它正在选择选项,但该值从未作为输入发送,并且当我尝试继续时应用程序抛出错误。

以不同的方式尝试了 xpath!

WebElement st_1 = driver.findElement(By.id("app-select-no"));
 ((JavascriptExecutor)driver).executeScript("arguments[0].checked = true;", st_1);

下面是 HTML

div class="form-group input-form-group col-xs-6">
<input id="app-select-no" class="form-control ng-pristine ng-untouched ng-valid ng-valid-required" type="radio" ng-value="false" ng-model="openAccount.abc" required="" name="affRadioGroup" value="false" aria-checked="true" tabindex="0" aria-required="false" aria-invalid="false"/>
<label class="field-label-radio text-bold active" ng-class="{active : openAccount.abc==false}" for="app-select-no">
<span translate-once="NO_BUTTON">No</span>
</label>
</div>

【问题讨论】:

  • 恐怕我没有得到这部分“但该值从未作为输入发送”,您能详细说明一下吗?您是说正在选择该选项,但我没有得到问题所在。你也可以发布抛出的异常吗?

标签: javascript selenium radio-button executor


【解决方案1】:

让我看看如果我理解,您正试图在该元素上选择“是”或“否”,但我看到您单击的元素是一个输入。除了单击元素之外,您还需要将“值”发送到输入。

我猜你的问题不是执行脚本的点击,但你也可以试试这个:

WebElement st_1 = driver.findElement(By.id("app-select-no"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", st_1 );

要将值发送到该输入,您需要这样做:

driver.findElement(By.id("st_1")).sendKeys("value here");

希望对您有所帮助。

【讨论】:

  • 输入只是一个单选按钮,因此不需要发送键。如果我们只应用 javascript .checked=true,它应该可以工作,但是我们需要在执行 javascript 之前等待。已发布答案。
  • 没错,我没有看到html上的@type属性。
  • 使用 JavaScript 执行器我可以选择单选按钮,但该选项未在后端呈现,这不允许我从该页面移动! @Satish:我也用过等待。
  • @Joe 此代码正在选择单选按钮,现在该信息如何从 UI 传递到后端,实际上取决于设计。在 Selenium 中,我们只能确保选中单选按钮。尝试使用 webelement 方法使用第二个选项,然后等待:等待单选按钮被选中。
【解决方案2】:

在执行 javascript 之前放置等待元素解决了这个问题。这是使用 javascript 和不使用 javascript(使用 webelement click)的工作代码:

driver.get("file:///home/sgupta/Desktop/a.html");
    WebElement radio = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input#app-select-no")));
    JavascriptExecutor executor = (JavascriptExecutor)driver.getDriver();
    executor.executeScript("arguments[0].checked=true", radio);

使用 WebElement 方法:

driver.get("file:///home/sgupta/Desktop/a.html");
WebElement radio = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input#app-select-no")));
radio.click();

【讨论】:

    猜你喜欢
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 2019-06-06
    • 1970-01-01
    • 2015-07-20
    • 2014-03-31
    相关资源
    最近更新 更多