我是Selenium Webdriver的新手。我正在使用Java编写脚本,但是我被困在编写脚本中,我想选择一个单选按钮,但是
线程“主要” org.openqa.selenium.ElementNotVisibleException中的异常:未显示元素(警告:服务器未提供任何堆栈跟踪信息)错误。
这是我写的:
WebElement radioButton = driver.findElement(By.id("Radio_0_2461A"));
radioButton.sendKeys(Keys.SPACE);
以下是HTML代码:
HTML code for Find button.
<button class="button right secondary" id="selectProducts0" onclick="getProducts('0'); return false;">
Find Products
</button>
单击按钮后,将加载产品,我必须从列表中选择产品。
<div class="clearboth" id="productList0" style="overflow: hidden; display: block;">
<div class="productTableContainer" data-index="0">
<table class="responsive" cellspacing="0" cellpadding="0" summary="List of products">
<tbody>
<tr data-row-data='{"earlyRepaymentCharges":[],"incentives":[]}'>
<th class="radio">
<input id="0_2689A_StartDate" type="hidden" value="28/06/2016 00:00:01">
<input name="Products[0].ProductCode" title="Lifetime Tracker with £999 Fee" id="Radio_0_2689A" type="radio" value="2689A"> <label for="Radio_0_2689A">
Lifetime Tracker with £999 Fee </label>
<tr data-row-data='{"earlyRepaymentCharges":[{"step":"1","durationInMonths":"","endDate":"30/11/2016","percentage":"1%"}],"incentives":[]}'>
<th class="radio">
<input id="0_5555A_StartDate" type="hidden" value="01/11/2015 00:01:00">
<input name="Products[0].ProductCode" title="1 Year Fixed Rate Until 30/11/2016 with £999 Fee" id="Radio_0_5555A" type="radio" value="5555A">
<label for="Radio_0_5555A"> 1 Year Fixed Rate Until 30/11/2016 with £999 Fee
</label>
<tr data-row-data='{"earlyRepaymentCharges":[{"step":"1","durationInMonths":"","endDate":"28/02/2017","percentage":"2%"},{"step":"2","durationInMonths":"","endDate":"28/02/2018","percentage":"1%"}],"incentives":[]}'>
<th class="radio">
<input id="0_2461A_StartDate" type="hidden" value="18/12/2015 00:01:00">
<input name="Products[0].ProductCode" title="2 Year Fixed Rate Until 28/02/2018 with £999 Fee" id="Radio_0_2461A" type="radio" value="2461A">
<label for="Radio_0_2461A">2 Year Fixed Rate Until 28/02/2018 with £999 Fee
</label>
我认为单选按钮是动态生成的。尝试By.name()
与withWebDriverWait
一起使用,直到单选按钮如下所示:-
WebDriverWait wait = new WebDriverWait(driver,10);
WebElement radio = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("Products[0].ProductCode")));
radio.click();
Edited1:-如果单选按钮ID是固定的,则尝试使用By.id
withWebDriverWait
直到单选按钮可见,如下所示:-
WebDriverWait wait = new WebDriverWait(driver,10);
WebElement radio = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Radio_0_2461A")));
radio.click();
Edited2:-如果您可以找到单选按钮,但由于可见性而无法选择,则可以尝试使用JavascriptExecutor
来选择单选按钮,如下所示:-
WebDriverWait wait = new WebDriverWait(driver,10);
WebElement radio = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("Radio_0_2461A")));
((JavascriptExecutor)driver).executeScript("arguments[0].click()", radio);
Edited3:-如果不幸的是,javascript单击在单选按钮上不起作用,请尝试使用Javascript
Mouse
事件执行单击,如下所示:-
WebDriverWait wait = new WebDriverWait(driver,10);
WebElement radio = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("Radio_0_2461A")));
((JavascriptExecutor)driver).executeScript("var clickEvent = document.createEvent('MouseEvents');clickEvent.initEvent ('click', true, true);arguments[0].dispatchEvent (clickEvent);", radio);
希望能帮助到你...:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句