【问题标题】:Not Able to click in Radio Button - Selenium无法单击单选按钮 - Selenium
【发布时间】:2021-10-28 09:34:07
【问题描述】:

尝试从单选按钮中选择,在控制台上检查元素是否存在并且它不在 Iframe 下

$x("//*[@id='featuresCollapse1']/div[1]/label/span")
    (2) [span.overlay, span.overlay]

HTML:

<div class="col-6 my-1 feature-input-wrapper   text-left">
<input name="radio0" data-index="1" id="Personal Accident for Driver only" hidden="" type="radio"><label data-code="PA-DO" data-value="60" class="radio-inline custom-component featureInput p-0 radio-no form-group">
<span class="overlay" tabindex="0"></span> 
<label data-value="60" class="feature-name">Personal Accident for Driver only<span class="option-price font-weight-bold ml-2"> 60 </span> </label></label></div>

收音机的 Xpath 是:private final By PAforDriverOnly = By.xpath("//*[@id='featuresCollapse1']/div[1]/label/span");

在这里尝试检查它是否显示..但它不是“读取异常消息”

try {
    driver.findElement(PAforDriverOnly).isDisplayed();
    System.out.println("driver.findElement(PersonalAccindent).isDisplayed();");
    String Persional = driver.findElement(PAforDriverOnly).getText();
    System.out.println(Persional);
    driver.findElement(PAforDriverOnly).click();
    System.out.println("driver.findElement(PersonalAccindent).click();");
    //getTextAct(ProceedToCheckoutBtn);
} catch (Exception e){
    System.out.println("The Exception Message of Verify Button : "+e);
}

这里尝试使用 JavaScriptExcutor,但在 WebDriverWait 上失败, 一次删除它,它会读取 JavaExcutor 但不执行任何操作:

WebElement myElement = driver.findElement(PAforDriverOnly);
new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(PAforDriverOnly));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", myElement );
System.out.println("JavaScriptExcutor -  Click on RadioButton");

    Thread.sleep(2000);
    Btnclick(PAforDriverOnly);

【问题讨论】:

  • 首先System.out.println("driver.findElement(PersonalAccindent).isDisplayed();"); 将准确打印文本'driver.findElement(PersonalAcindent).isDisplayed();'不是driver.findElement(PersonalAccindent).isDisplayed(); 作为Boolean 的结果。其次你得到什么例外?可以提供网址吗?
  • @pburgr : 看看他过去的记录,他会自己回答的。
  • @pburgr 是的,我这样做是为了检查它是否可以显示。这很好,但我的问题是为什么不能选择单选按钮 .. 尝试了两种方式跨度>
  • @cruisepandey 的意思是什么?

标签: java selenium selenium-webdriver automation webdriver


【解决方案1】:

我希望你的 xpath 是错误的, 因为您的 xpath 指向标签。尝试将其指向 input 标签。

使用此 //input[@id='Personal Accident for Driver only'] 或在查找元素的过程中使用它,但您需要深入了解输入标签。

【讨论】:

  • 在失败之前尝试过那个Expected condition failed: waiting for visibility of element located by By.xpath: //input[@id='Personal Accident for Driver only'] ...一旦选择单选按钮上的元素显示元素的Xpath是//*[@id="featuresCollapse1"]/div[1]/label/span@Jayanth Bala
猜你喜欢
  • 1970-01-01
  • 2021-08-13
  • 2019-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
  • 1970-01-01
相关资源
最近更新 更多