我是selenium webdriver的新手。我正在使用java编写脚本,但我在编写脚本时遇到困难,我想选择一个单选按钮但是我正在
线程“main”中的异常 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>
答案 0 :(得分:1)
我认为单选按钮I是动态生成的。尝试将By.name()
与WebDriverWait
一起使用,等到单选按钮可见,如下所示: -
WebDriverWait wait = new WebDriverWait(driver,10);
WebElement radio = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("Products[0].ProductCode")));
radio.click();
已修改: - 如果单选按钮ID已修复,请尝试使用By.id
与WebDriverWait
同等,直到单选按钮可见,如下所示: -
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);
希望它有帮助...:)