0

I am having problems with being able to select the RUC option in this software with selenium, the labels do not contain ids that differentiate them and the classes are the same:

HTML elements

I am using the following code but it doesn't work for me:

label_tipo_documento = driver.find_element_by_xpath('//div[@id="form-group-tipo_documento"]//div[@class="col-sm-3"]//label[@class="radio-inline"][2]')
print(label_tipo_documento.text)
label_tipo_documento.click()

I try to select the 2nd option within the labels [2] (RUC) and I get the following message:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@id="form-group-tipo_documento"]//div[@class="col-sm-3"]//label[@class="radio-inline"][2]"}
  (Session info: chrome=87.0.4280.88)

However, when I do it with [1] instead of [2], it finds a value:

DNI
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
DJG
  • 29
  • 4

4 Answers4

0

Use following xpath to click on 2nd radio button.

label_tipo_documento = driver.find_element_by_xpath("//label[./input[@value='RUC']]")
print(label_tipo_documento.text)
label_tipo_documento.click()

If yon want to use indexing then try this code.

label_tipo_documento = driver.find_element_by_xpath('(//div[@id="form-group-tipo_documento"]//div[@class="col-sm-3"]//label[@class="radio-inline"])[2]')
print(label_tipo_documento.text)
label_tipo_documento.click()
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • This would work, might be better to use find_elements_by_xpath and then do the indexing in python rather than xpath. – DMart Dec 10 '20 at 16:32
0

To click on the element associated with the text as RUC you can use either of the following Locator Strategies:

  • Using css_selector for the <input>:

    driver.find_element_by_css_selector("div#form-group-tipo_documento label.radio-inline > input[value='RUC']").click()
    
  • Using xpath and <label> text:

    driver.find_element_by_xpath("//div[@id='form-group-tipo_documento']//label[@class='radio-inline' and contains(., 'RUC')]/input").click()
    

Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#form-group-tipo_documento label.radio-inline > input[value='RUC']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='form-group-tipo_documento']//label[@class='radio-inline' and contains(., 'RUC')]/input"))).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
    

References

You can find a couple of relevant discussions on NoSuchElementException in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
-1
//div[@id="form-group-tipo_documento"]//div[@class="col-sm-3"]//label[@class="radio-inline"][2]

Means that you want to select second descendant label of div node (it has only one descendant label). While you need to select second occurrence of label. So either try

label_tipo_documento = driver.find_element_by_xpath('(//div[@id="form-group-tipo_documento"]//div[@class="col-sm-3"]//label[@class="radio-inline"])[2]')

or

label_tipo_documento = driver.find_elements_by_xpath('//div[@id="form-group-tipo_documento"]//div[@class="col-sm-3"]//label[@class="radio-inline"]')[1]
JaSON
  • 4,843
  • 2
  • 8
  • 15
-1

Good morning.

So, based on your image, I believe the xpath that you would need is this.

# XPATH
//div[contains(@id, 'tipo_documento')]//label[@class='radio-inline']//input[@value='DNI']

Based off of this xpath, you should make a variable for the radio button that you want to click and then execute it with the following command.

myValue = "DNI"
driver.find_element(By.XPATH, "//div[contains(@id, 'tipo_documento')]//label[@class='radio-inline']//input[@value='{0}']".format(myValue)).click()

Based off of this code, you could change "DNI" to "RUC" or "CEX" and you should be good to go.