【问题标题】:Selenium: Not able to select the radio buttonSelenium:无法选择单选按钮
【发布时间】:2020-09-24 15:15:13
【问题描述】:

我正在尝试使用 selenium 选择一个单选按钮,当我运行脚本时 selenium 没有选择单选按钮,而是在不选择单选按钮的情况下进入下一步而不给我任何错误。

下面是相同的 HTML:

<div class="col-12 scrolling pend active" id="mergeTransTable"> 
    <div class="title-table transactions">
        <div class="expenseDetailId" style="cursor: pointer;">
            <input id="hdnMergeOtherDataCount" name="hdnMergeOtherDataCount" type="hidden" value="0">
        </div>                                                                       
        <div class="comment" style="cursor: pointer;" width="20%">Comment
        <div class="vendor" style="cursor: pointer;">Vendor </div>                
        </div>
            <div class="receipt" style="cursor: pointer;">
                Receipt
        </div>            
    </div>            
    <div class="data-table transactions">
        <div class="expenseDetailId" data-title="ExpenseDetailId" style="margin-top:2%;">
            <input type="radio" name="ExpenseDetailId" id="ExpenseDetailId" class="chk" value="163867">
        </div>
        <div class="vendor" data-title="Vendor">AMAZON</div>                
        <div class="comment" data-title="Comment" width="20%">
            UBER
        </div>              
             <div class="receipt&quot;" data-title="Receipt">                    
                <a id="Receipt" href="abc.com" onclick="return ViewReceipt('/ExpenseDetailId=163867')">Receipt</a>                     
        </div>
    </div>
</div>

下面是我为选择单选按钮编写的 xpaths

driver.findElement(By.xpath("//*[@id='mergeTransTable']/div[2]/div[1]")).click();
driver.findElement(By.xpath("//div[@id='mergeTransTable']//div[@class='expenseDetailId' or starts-with(@data-title,'ExpenseDetailId')]")).click();

使用 CSS 也无济于事。

也尝试过 Java Script 和 Action 类,但似乎没有任何效果。 Selenium 只是不要选择单选按钮

【问题讨论】:

    标签: css selenium selenium-webdriver xpath radio-button


    【解决方案1】:

    点击输入而不是div标签。

    //*[@id='mergeTransTable']/div[2]/div[1]/input
    

    【讨论】:

      【解决方案2】:

      查看您的代码,您似乎在“单击”div 而不是单选按钮。因此没有任何反应,也没有错误消息。为什么不直接点击id为ExpenseDetailId的元素呢?

      driver().findElement(By.id("ExpenseDetailId")).click();

      【讨论】:

      • 这不起作用? driver().findElement(By.id("ExpenseDetailId")).click();
      猜你喜欢
      • 2021-03-31
      • 1970-01-01
      • 2018-09-17
      • 2018-07-08
      • 1970-01-01
      • 2021-10-28
      • 2015-07-01
      相关资源
      最近更新 更多