无法单击 HTML 标签内的输入类型 = "radio"?

Cannot click on input type = "radio" that is inside a HTML label?

我遇到一个问题,我无法使用 id="same-supplier-no":

单击输入类型 = "radio"
<label for="same-supplier-no" ng-class="{checked: !ctrl.energyModel.BillViewModel.SameSupplier}" class="">

  <input type="radio" id="same-supplier-no" name="same-supplier" ng-model="ctrl.energyModel.BillViewModel.SameSupplier" ng-value="false" ng-change="ctrl.sameSupplierValueChanged(); ctrl.analyticsProvider.sendVirtualPageView('SameSupplierNo')" class="ng-pristine ng-untouched ng-valid" value="false">

   <span></span>No

</label>

在 Selenium 中,当我这样做时:

driver.findElement(By.id("same-supplier-no")).click();

它说 "element not visible"。 HTML 中有以前的单选按钮,上面有标签,它们有标签的 ID(这个没有)。 我只是参考标签 ID 并单击它,它就起作用了。但是这个没有标签的 ID,我想避免使用 xpath,但是,如果我别无选择,那么我将通过 xpath/css.

select

我也尝试过通过定位器名称,即 By.className("same-supplier");并使用 For 循环和 if 语句来存储输入 type=radio 元素,仍然不起作用。

问题是:为什么我不能点击input type = "radio",为什么Selenium不可见?是因为它嵌套在 Label 中吗?如果是这样,有谁知道我如何使用 id 或直接访问元素来单击它?

谢谢。

注意:我对 Selenium 使用 Java 7。

使用操作点击标签,然后使用:

Actions actions = new Actions(driver);
                IWebElement menuHoverLink = driver.findElement(By.XPath("//label[@for='same-supplier-no']"));
                actions.moveToElement(menuHoverLink);
                actions.click();
                actions.perform();

driver.findElement(By.id("same-supplier-no")).click();

从聊天对话中,我可以在您使用的网站上尝试一些东西。 radio 永远不会变得可见。因此,不可能在其上使用 click() 方法。

但是包含这个radiolabel是可见的。这意味着您可以在此标签上使用 click()

driver.findElement(By.cssSelector("label[for='same-supplier-no']")).click();

或者如果您真的想使用 radio 查找元素,您可以使用 xpath.

driver.findElement(By.xpath("//*[@id='same-supplier-no']/parent::label")).click();

点击"find postcode"按钮后,添加下面两行代码-

WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath(".//*[@id='same-supplier-question']/div/div/label[2]/span"))));

这对我有用... 您可以将 xpath 从绝对更改为相对....