简体   繁体   English

单击单选按钮硒

[英]Click Radio Button Selenium

Been trying to click a radio button on website to no avail. 试图单击网站上的单选按钮无济于事。

 <df-radio-group class="o-flex o-flex--distribute df-question ng-tns-c33-15 ng-has-value ng-touched ng-dirty ng-valid" aria-label="Title" id="title" aria-labelledby="title" role="radiogroup" style="float: left; width: 100%;"> <!----><df-radio _nghost-c40="" class="ng-tns-c33-15 is-checked" id="md-radio-93fd918"><label _ngcontent-c40="" class="df-radio-label" for="md-radio-93fd918-input"> <input _ngcontent-c40="" class="df-radio-input visually-hidden" type="radio" id="md-radio-93fd918-input" name="df-radio-group-0"> <div _ngcontent-c40="" class="md-radio-label-content"> Mr </div> </label></df-radio><df-radio _nghost-c40="" class="ng-tns-c33-15" id="md-radio-bd05b81"><label _ngcontent-c40="" class="df-radio-label" for="md-radio-bd05b81-input"> <input _ngcontent-c40="" class="df-radio-input visually-hidden" type="radio" id="md-radio-bd05b81-input" name="df-radio-group-0"> <div _ngcontent-c40="" class="md-radio-label-content"> Mrs </div> </label></df-radio><df-radio _nghost-c40="" class="ng-tns-c33-15" id="md-radio-ba9f195"><label _ngcontent-c40="" class="df-radio-label" for="md-radio-ba9f195-input"> <input _ngcontent-c40="" class="df-radio-input visually-hidden" type="radio" id="md-radio-ba9f195-input" name="df-radio-group-0"> <div _ngcontent-c40="" class="md-radio-label-content"> Miss </div> </label></df-radio><df-radio _nghost-c40="" class="ng-tns-c33-15" id="md-radio-ec973a5"><label _ngcontent-c40="" class="df-radio-label" for="md-radio-ec973a5-input"> <input _ngcontent-c40="" class="df-radio-input visually-hidden" type="radio" id="md-radio-ec973a5-input" name="df-radio-group-0"> <div _ngcontent-c40="" class="md-radio-label-content"> Ms </div> </label></df-radio> </df-radio-group> 

Been trying to click both the radio button and the label but selenium keeps throwing the no such element error and I'm kinda frustrated at this stage. 一直试图单击单选按钮和标签,但是硒一直没有引发此类元素错误,在这个阶段,我有点沮丧。

Might be easier to see on the actual website: 在实际的网站上可能更容易看到:

https://www.theaa.ie/car-insurance/journey/getting-started https://www.theaa.ie/car-insurance/journey/getting-started

Its on the page after entering the email. 输入电子邮件后在页面上。 Trying to get some test cases going but these radio buttons dont want to be clicked. 试图进行一些测试用例,但这些单选按钮不希望被单击。

Having a look to the html structure: 看一下html结构:

在此处输入图片说明

You could wait the presence of the web element with id "title" and then find all the elements with class name " md-radio-label-content ". 您可以等待id为“ title”的web元素的出现,然后找到所有类名为“ md-radio-label-content ”的元素。

Once you have all of them, you can check the text and click the interested one. 一旦拥有所有这些内容,就可以检查文本并单击感兴趣的文本。

So, for example, if you want to click on "Mr": 因此,例如,如果您想单击“先生”:

WebElement titleRadiogroup = (new WebDriverWait(driver, 10))
                .until(ExpectedConditions.presenceOfElementLocated(By.id("title")));


        List<WebElement> ele= titleRadiogroup.findElements(By.className("md-radio-label-content"));

        for (WebElement el : ele)
        {
            if(el.getText().equalsIgnoreCase("Mr"))
            {
                el.click();
            }
        }

In cases like this, I would use an XPath to click on the element by its contents. 在这种情况下,我将使用XPath单击元素的内容。 One issue you run into here is that these DIV s that contain the titles are full of whitespace (see below). 您在这里遇到的一个问题是,这些包含标题的DIV充满了空格(请参见下文)。

<div _ngcontent-c40="" class="md-radio-label-content">

            Mr

</div>

Because of this, we can't do something simple like 因此,我们不能做简单的事情

//div[.='Mr']

Another issue is that you can't use contains() because Mr and Mrs both contain the string Mr . 另一个问题是您不能使用contains()因为MrMrs都包含字符串Mr We can avoid the whole thing by using normalize-space() which trims whitespace. 通过使用修剪空白的normalize-space() ,我们可以避免整个事情。

//div[normalize-space(.)='Mr']

If it were me, I would wrap this in a function since you are likely to use it repeatedly. 如果是我,我会将其包装在一个函数中,因为您可能会重复使用它。

public void selectTitle(String title)
{
    driver.findElement(By.xpath("//div[normalize-space(.)='" + title + "']")).click();
}

and call it like 并称它为

selectTitle("Miss");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM