0

I want to click the radio button of the item that matches my grant number. The issue is that the HTML seems to have some numbers before it that I don't know until I search. For example, if I search for ABC-2202, the HTML might look something like this:

<input type="Radio" name="PanelGroup" id="PanelGroup" value="225,ABC-2202" title="Panel Group">

I want to find and click the radio button that ends with my phrase because there also exists some results like ABC-2202-A, ABC-2202-B, etc.

I have already tried the following (which I found in other questions on Stackoverflow), with the variable:

grant = 'ABC-2202'

then

driver.find_element(By.XPATH,"//input[substring(@value, string-length(@value) - string-length(grant) +1) = grant]".click()

and

driver.find_element(By.CSS_SELECTOR, "input[type='radio'][value$=grant]").click()

but neither of these has worked for me.

Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24
  • How many buttons are there? Can't you use `find_elements` for all `` tags and just search the `value` attribute yourself? – Tim Roberts Jun 26 '23 at 18:00
  • contains or ends-with should work... see this thread: https://stackoverflow.com/questions/40934644/xpath-testing-that-string-ends-with-substring – pcalkins Jun 26 '23 at 18:25

1 Answers1

0

Selenium still supports only where ends-with() isn't supported.


Solution

Given the following HTMLs:

<input type="Radio" name="PanelGroup" id="PanelGroup" value="225,ABC-2202" title="Panel Group">

and

<input type="Radio" name="PanelGroup" id="PanelGroup" value="ABC-2202-A" title="Panel Group">

and

<input type="Radio" name="PanelGroup" id="PanelGroup" value="ABC-2202-B" title="Panel Group">   

Using Selenium to identify the element where the value attribute ends with ABC-2202 a potential solution would be to use a css_selector as follows:

input[value$='ABC-2202']

Accomodating the other attributes:

input#PanelGroup[value$='ABC-2202'][type='Radio'][name='PanelGroup'][title='Panel Group']

Ideally, to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategy:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#PanelGroup[value$='ABC-2202'][type='Radio'][name='PanelGroup'][title='Panel Group']"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352