java - 如何在 selenium webdriver 中选择单选按钮?

标签 java selenium selenium-webdriver

我是 selenium webdriver 的新手。我正在使用 java 编写脚本,但我陷入了编写脚本的困境,我想选择一个单选按钮,但我得到了

Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not displayed (WARNING: The server did not provide any stacktrace information) error.

这是我写的:

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()WebDriverWait 等待单选按钮可见,如下所示:-

WebDriverWait wait = new WebDriverWait(driver,10);
WebElement radio = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("Products[0].ProductCode")));
radio.click();

Edited1 :- 如果您的单选按钮 ID 已修复,请尝试将 By.idWebDriverWait 一起使用,直到单选按钮可见,如下所示:-

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

Edit3:- 如果不幸的是 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);

希望有帮助...:)

关于java - 如何在 selenium webdriver 中选择单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38703744/

相关文章:

java - 使用 PDFBox 获取文本颜色

javascript - Protractor :无法运行测试

python - 如何将FirefoxWebElememt转换为字符串类型?

java - 如何使用一致的数据针对 Oracle UCM 执行集成测试?

ruby - 使用 Test::Unit::Autorunner, "assert_true"无方法错误

java - 如何在测试运行时保持 'Developer Tool' 窗口打开

java - spring jpa @Query 错误,期待关闭,发现附近有 '('

java - eclipse 插件中的 SWT 浏览器在 Mac 和 Windows 中的行为不同

java - 将菜单项或菜单添加到Java中的JTable列标题中

python - 使用 Python Selenium webdriver 登录雅虎电子邮件帐户