簡體   English   中英

使用Javascript執行器選擇單選按鈕

[英]Radio Button selection using Javascript executor

我正在嘗試在頁面中選擇“是”“否”選項,正常情況下,“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>

讓我看看如果我理解,你試圖在該元素上選擇“是”或“否”,但我看到你點擊的元素是一個輸入。 除了單擊元素之外,您還需要將“值”發送到輸入。

我猜你的問題不是使用executeScript的點擊,但你也可以試試這個:

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之前放置等待元素解決了問題。 這是使用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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM