本文介绍了填充伪::: before&的单选选项使用Selenium WebDriver的:: after元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在确定选择哪个单选选项时,我需要一些帮助.我正在处理的网页有两个单选选项是"和否".下面是选择是"时的HTML代码.请注意,::after填充在label for标记中.

I need some help with identifying which radio option is selected. The webpage I am dealing with has two radio options Yes and No. Below is the HTML code when option Yes is selected. Note that ::after is populated in the label for tag for it.

<div class="rule-scope-radio-list">
    <label class="vdl-radio">
        <input id="radio_HlzYBaCpA3xEjRkH0fgU-" type="radio" class="vdl-radio__input" tabindex="0" aria-disabled="false" value="true">
        <label for="radio_HlzYBaCpA3xEjRkH0fgU-">
          ::before
          "Yes"
          ::after
        </label>
    </label>
    <label class="vdl-radio">
        <input id="radio_KHhle2RxBSrl5Vb_n7Eit" type="radio" class="vdl-radio__input" tabindex="0" aria-disabled="false" value="false">
        <label for="radio_KHhle2RxBSrl5Vb_n7Eit">
          ::before
          "No"
        </label>
    </label>
</div>

在网页上选择否"时,仅将HTML代码中更改为::after的内容从是"选项中删除,并填充为否"选项.

When option No is selected on the webpage only thing that changes in the HTML code is ::after is removed from Yes option and gets populated for No option.

我尝试了Selenium的isSelected()方法,但是当我希望Yes选项为'true'时,它总是返回'false'.

I tried Selenium's isSelected() method but it always returned 'false' when I am expecting 'true' for Yes option.

作为第二种选择,我在Java代码下面编写了同样没有用的代码.它返回null.我的想法是获取整个input for标记,然后使用string.contains()方法检查其中是否包含::after.因为当我在浏览器控制台上的JavaScript下执行时,它返回了整个input for标记,其中同时包含::before::after元素.

As a second alternative I wrote below Java code that also didn't work. It is returning null. My idea was to get the entire input for tag and then use string.contains() method to check if it has ::after in it. Because when I execute below JavaScript on browser console, it returned entire input for tag with both ::before and ::after elements in it.


public String yesOrNoSelected(){
        String tag = "";
        List<WebElement> radioOptions = findElementsByXpath(".//div[@class='rule-scope-radio-list']/label");

        //Iterate thru both radio options and execute the JavaScript.

        for(int i = 1; i <= radioOptions.size(); i++) {
                String script = "return document.querySelector('div#rule-scope-radio-list> label:nth-of-type("+i+") label', null);";
                JavascriptExecutor js = (JavascriptExecutor) driver;
                tag = (String) js.executeScript(script);
                System.out.println(tag);
            }
        return tag;
    }
}

推荐答案

我从不尝试使用Java进行Selenium,但是在这里我为您提供了一些代码.我不确定该行不行.但是我建议您使用cssSelector来检查是否已选中RadioButton.

I never try Selenium with java but here i do some code for you. I am not damn sure it will work or not.but i suggest you to use cssSelector for checking RadioButton is checked or not.

public String yesOrNoSelected(){
    String tag = "";
    List<WebElement> radioOptions = findElementsByXpath("//div[@class='rule-scope-radio-list']");

    //Iterate thru both radio options and execute the JavaScript.

    for(int i = 1; i <= radioOptions.size(); i++) {
    var val = radioOptions.get(i).findElement(By.cssSelector("input[value='true']"));
    if(val!=null){
        tag = radioOptions.get(i).getTagName();
        System.out.println(tag);
    }
  }
  return tag;
}}

这篇关于填充伪::: before&amp;的单选选项使用Selenium WebDriver的:: after元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 06:17