如何选择Selenium WebDriver中的单选按钮?

Prashant Bellale

我是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>
索拉布·高尔(Saurabh Gaur)

我认为单选按钮是动态生成的。尝试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.idwithWebDriverWait直到单选按钮可见,如下所示:-

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何检查Selenium WebDriver中是否选择了单选按钮?

来自分类Dev

如何检查Selenium WebDriver中是否选择了单选按钮?

来自分类Dev

无法检查Selenium WebDriver中的单选按钮

来自分类Dev

无法单击Selenium Webdriver中的单选按钮

来自分类Dev

如何通过Selenium Webdriver用Python选择Twitter中的单选按钮?

来自分类Dev

如何使用带有Java的Selenium WebDriver选择单选按钮?

来自分类Dev

使用Selenium Webdriver选择PrimeFaces单选按钮

来自分类Dev

在Selenium Webdriver [C#]中选择单选按钮

来自分类Dev

如何在Selenium Web Driver中的一组类似的单选按钮中选择特定的单选按钮?

来自分类Dev

HTML中的单选按钮选择

来自分类Dev

Android中的单选按钮选择

来自分类Dev

HTML中的单选按钮选择

来自分类Dev

如何使用Selenium和Python选择单选按钮

来自分类Dev

如何在Selenium Python中选择单选按钮?

来自分类Dev

Ruby-Selenium Webdriver,AngularJS单选按钮无法检测属性

来自分类Dev

如何隐藏单选按钮列表中的单选按钮之一?

来自分类Dev

Java:如何避免单选按钮中的多个选择。

来自分类Dev

如何使用单选按钮选择jsp中的特定行数据

来自分类Dev

如何在asp.net中默认选择单选按钮

来自分类Dev

如何在Django中访问单选按钮选择的值

来自分类Dev

如何在asp.net中默认选择单选按钮

来自分类Dev

选择单选按钮后如何管理android中的布局?

来自分类Dev

Selenium C#中的单选按钮

来自分类Dev

从多个单选组中随机选择单选按钮

来自分类Dev

通过Selenium Java选择单选按钮

来自分类Dev

UI中的单选按钮选择未刷新

来自分类Dev

选择表单中的特定单选按钮

来自分类Dev

在Shiny R中更新单选按钮选择

来自分类Dev

iOS中的单选按钮选择图像

Related 相关文章

  1. 1

    如何检查Selenium WebDriver中是否选择了单选按钮?

  2. 2

    如何检查Selenium WebDriver中是否选择了单选按钮?

  3. 3

    无法检查Selenium WebDriver中的单选按钮

  4. 4

    无法单击Selenium Webdriver中的单选按钮

  5. 5

    如何通过Selenium Webdriver用Python选择Twitter中的单选按钮?

  6. 6

    如何使用带有Java的Selenium WebDriver选择单选按钮?

  7. 7

    使用Selenium Webdriver选择PrimeFaces单选按钮

  8. 8

    在Selenium Webdriver [C#]中选择单选按钮

  9. 9

    如何在Selenium Web Driver中的一组类似的单选按钮中选择特定的单选按钮?

  10. 10

    HTML中的单选按钮选择

  11. 11

    Android中的单选按钮选择

  12. 12

    HTML中的单选按钮选择

  13. 13

    如何使用Selenium和Python选择单选按钮

  14. 14

    如何在Selenium Python中选择单选按钮?

  15. 15

    Ruby-Selenium Webdriver,AngularJS单选按钮无法检测属性

  16. 16

    如何隐藏单选按钮列表中的单选按钮之一?

  17. 17

    Java:如何避免单选按钮中的多个选择。

  18. 18

    如何使用单选按钮选择jsp中的特定行数据

  19. 19

    如何在asp.net中默认选择单选按钮

  20. 20

    如何在Django中访问单选按钮选择的值

  21. 21

    如何在asp.net中默认选择单选按钮

  22. 22

    选择单选按钮后如何管理android中的布局?

  23. 23

    Selenium C#中的单选按钮

  24. 24

    从多个单选组中随机选择单选按钮

  25. 25

    通过Selenium Java选择单选按钮

  26. 26

    UI中的单选按钮选择未刷新

  27. 27

    选择表单中的特定单选按钮

  28. 28

    在Shiny R中更新单选按钮选择

  29. 29

    iOS中的单选按钮选择图像

热门标签

归档