无法访问单选按钮

时间:2018-06-19 15:02:09

标签: c# selenium

我有HTML波纹管,我想单击输入名称= optUsoTabaco的单选按钮。但是我没有成功。

<section id="bloco-cadastro" ng-show="vm.modo.edicao || vm.modo.novo" class="">
        <ul class="passos">
        </ul>
        <form name="formSimulador" novalidate="" class="ng-valid-pattern ng-valid-maxlength ng-valid-cpf ng-invalid ng-invalid-required ng-dirty ng-valid-parse ng-valid-date">
            <p class="aviso-campos-assinalados text-muted">Os campos assinalados com * são obrigatórios.</p>
            <h3 class="text-info titulo-bloco page-header">Dados do Cliente</h3>
            <fieldset class="simulador small">
                <input type="hidden" name="hidSimulacaoId" ng-model="vm.viewmodelsimulacao.simulacaoId" class="ng-pristine ng-untouched ng-valid ng-empty">
                <fieldset ng-disabled="!vm.viewmodelsimulacao.formularioEditavel">
                    <div class="row passo-1" id="passoUm">                       
                    </div>
                    <div class="row dados-solicitante">
                    </div>
                    <div class="row dados-estudo">
                    </div>
                    <div class="row">
                        <div class="col-xs-12 col-sm-6 col-md-6 form-group">
                            <label>Sexo *</label>
                            <div class="radio md-radio">
                                <label class="separador_direita  ativo"><input type="radio" value="F" ng-model="vm.viewmodelsimulacao.sexo" name="optSexo" required="" class="ng-touched ng-not-empty ng-dirty ng-valid-parse ng-valid ng-valid-required">Feminino</label>
                                <label class="separador_esquerda "><input type="radio" value="M" ng-model="vm.viewmodelsimulacao.sexo" name="optSexo" required="" class="ng-pristine ng-untouched ng-not-empty ng-valid ng-valid-required">Masculino</label>
                            </div>
                            <div class="has-error ng-hide" ng-show="formSimulador.$submitted || formSimulador.optSexo.$touched">
                                <span class="control-label ng-hide" ng-show="formSimulador.optSexo.$invalid">
                                    Valor Obrigatório
                                </span>
                            </div>
                        </div>
                        <div class="col-xs-12 col-sm-6 col-md-6 form-group">
                            <label>Fumante? *</label>
                            <div class="radio md-radio">
                                <label class="separador_direita "><input type="radio" value="S" ng-model="vm.viewmodelsimulacao.fazUsoDeTabaco" name="optUsoTabaco" required="" class="ng-pristine ng-untouched ng-empty ng-invalid ng-invalid-required">Sim</label>
                                <label class="separador_esquerda "><input type="radio" value="N" ng-model="vm.viewmodelsimulacao.fazUsoDeTabaco" name="optUsoTabaco" required="" class="ng-pristine ng-untouched ng-empty ng-invalid ng-invalid-required">Não</label>
                            </div>
                            <div class="has-error ng-hide" ng-show="formSimulador.$submitted || formSimulador.optUsoTabaco.$touched">
                                <span class="control-label" ng-show="formSimulador.optUsoTabaco.$invalid">
                                    Valor Obrigatório
                                </span>
                            </div>
                        </div>
                    </div>

                </fieldset>
                <!-- ngIf: vm.botoesAcao --><div class="row ng-scope" ng-if="vm.botoesAcao">

            </fieldset>

        </form>
    </section>

我试图访问名为optUsoTabaco的单选按钮,其代码为:

driver.FindElement(By.CssSelector("#bloco-cadastro > form > fieldset > fieldset > div:nth-child(4) > div:nth-child(2) > div.radio.md-radio > label.separador_direita.ativo"));

------ x ------        

driver.FindElement(By.XPath("//*[@id='bloco - cadastro']/form/fieldset/fieldset/div[4]/div[2]/div[1]/label[2]/input")).Click();

------ x ------        

 driver.FindElement(By.XPath("//*[@id='bloco - cadastro']/form/fieldset/fieldset/div[4]/div[2]/div[1]/label[2]")).Click();

------ x ------     

IList<IWebElement> radioBtn = driver.FindElements(By.Name("optUsoTabaco"));
radioBtn.ElementAt(1).Click();

在所有这些情况下,我得到错误:“ WebDriver.dll中发生了'OpenQA.Selenium.NoSuchElementException类型的异常,但未在用户代码中处理。附加信息:无此类元素:无法找到元素” 我在做什么错了?

我已经尝试等待10秒钟,但并不能解决问题

3 个答案:

答案 0 :(得分:0)

您正在编写的所有xpath和cssSelector都是绝对,使用起来不太合理。虽然它们是正确的。我看不出他们有任何问题。

我认为存在iframe / frameset涉及,因为您必须将Web驱动程序的焦点切换到特定的iframe。

您可以尝试使用的代码:

driver.SwitchTo().Frame(driver.FindElement(By.Xpath("xpath of frame")));

答案 1 :(得分:0)

您可以使用CSS选择器

如果要选择Sim单选按钮,请使用

 div.row > div > div > label > input[name="optUsoTabaco"][value="S"]

如果要选择Não,请使用

 div.row > div > div > label > input[name="optUsoTabaco"][value="N"]

答案 2 :(得分:0)

我使用JavaScript解决了问题

IWebElement st_2 = driver.FindElement(By.Name("optUsoTabaco"));
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", st_2);