-1

I am writing a script to count the number of radio buttons on the webpage - https://docs.google.com/forms/d/e/1FAIpQLSeI8_vYyaJgM7SJM4Y9AWfLq-tglWZh6yt7bEXEOJr_L-hV1A/viewform?formkey=dGx0b1ZrTnoyZDgtYXItMWVBdVlQQWc6MQ But it always throws me an error The common identifier between all the radio button is the role-radio.

<div class="appsMaterialWizToggleRadiogroupEl exportToggleEl isCheckedNext" jscontroller="D8e5bc" jsaction="keydown:I481le;dyRcpb:dyRcpb;click:cOuCgd; mousedown:UX7yZ; mouseup:lbsD7e; mouseenter:tfO1Yc; mouseleave:JywGue; focus:AHmuwe; blur:O22p3e; contextmenu:mg9Pef;touchstart:p6p2H; touchmove:FwuNnf; touchend:yfqBxc(preventMouseEvents=true|preventDefault=true); touchcancel:JMtRjd;" jsshadow="" aria-label="1" data-value="1" role="radio" aria-checked="false" aria-posinset="1" aria-setsize="5" tabindex="0"><div class="appsMaterialWizToggleRadiogroupInk exportInk"></div><div class="appsMaterialWizToggleRadiogroupInnerBox"></div><div class="appsMaterialWizToggleRadiogroupRadioButtonContainer"><div class="appsMaterialWizToggleRadiogroupOffRadio exportOuterCircle"><div class="appsMaterialWizToggleRadiogroupOnRadio exportInnerCircle"></div></div></div></div>
driver.get("https://docs.google.com/forms/d/e/1FAIpQLSeI8_vYyaJgM7SJM4Y9AWfLq-tglWZh6yt7bEXEOJr_L-hV1A/viewform?formkey=dGx0b1ZrTnoyZDgtYXItMWVBdVlQQWc6MQ")

ele=driver.find_elements_by_css_selector("input[role=radio]")

print(len(ele))

I am new to Selenium and this type of issues is really confusing. TIA

3 Answers3

0

try this selector

by_xpath("//div[@role='radio']")
by_css("div[role='radio']")

I recommend everybody to use CSS, supossedly is faster than xpath

Adrian Jimenez
  • 979
  • 9
  • 23
0

To count the number of radio buttons on the webpage you can use either of the following Locator Strategies:

  • Using class_name:

    driver.get("https://docs.google.com/forms/d/e/1FAIpQLSeI8_vYyaJgM7SJM4Y9AWfLq-tglWZh6yt7bEXEOJr_L-hV1A/viewform?formkey=dGx0b1ZrTnoyZDgtYXItMWVBdVlQQWc6MQ")
    print(len(driver.find_elements_by_class_name("appsMaterialWizToggleRadiogroupOffRadio")))
    
  • Using css_selector:

    driver.get("https://docs.google.com/forms/d/e/1FAIpQLSeI8_vYyaJgM7SJM4Y9AWfLq-tglWZh6yt7bEXEOJr_L-hV1A/viewform?formkey=dGx0b1ZrTnoyZDgtYXItMWVBdVlQQWc6MQ")
    print(len(driver.find_elements_by_css_selector("div.appsMaterialWizToggleRadiogroupOffRadio")))
    
  • Console Output:

    48
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Try any of this:

print(len(driver.find_elements_by_css_selector("exportOuterCircle")))
print(len(driver.find_elements_by_css_selector("exportInnerCircle")))

These will output:

48
48

You can try other css selector as well. The idea is to inspect element and find a unique class which is being used only for radio button and use that class in css selector. It will give you list of elements. You can then get the count of that list.

Kaushal Kumar
  • 1,237
  • 1
  • 12
  • 21