1

I am working with Selenium and Python. I am struggling to use the click() method in order to click a dynamically created radio button. The markup for the radio is below.

<input version="2" value="1" class="linked-ftb-radio meta(controlNumber=2)" id="radio_1" name="IndexString" reference="TEST 01" type="radio">
<label for="radio_1" id="linked-label" class="radio-label"></label>

The code I have is:

driver.find_element_by_xpath('//*[@id="radio_1"]').click()

However the following error is produced:

Traceback (most recent call last):
  File "index.py", line 41, in <module>
    driver.find_element_by_xpath('//*[@id="radio_1"]').click()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium/webdriver/remote/webelement.py", line 74, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium/webdriver/remote/webelement.py", line 457, in _execute
    return self._parent.execute(command, params)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 233, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with

The radio appears to be simulated by changing the image when the label is pressed. In other words, when clicked the class changes to radio-label selected.

radio

How can I click on the radio button with Selenium, bearing in mind it is not currently visible?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Alex
  • 151
  • 1
  • 2
  • 10
  • What happens if you add a pause before the click? (This is not the solution, only a further debug step) – Zoette Nov 29 '16 at 23:13
  • I think clicking on label would select the checkbox, you should once as `driver.find_element_by_css_selector('label[for="radio_1"]').click()` and let me know.. – Saurabh Gaur Nov 30 '16 at 03:23

2 Answers2

1

try explicit condition to wait for the element to be displayed. (if it is time-related issue, i.e., takes time to display)

 element = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.XPATH, "//*[@id='radio_1']"))
 element.click()

This waits up to 10 seconds before throwing a TimeoutException or if it is present on the DOM of a page and visible, will return it in 0 - 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.

Reference:

  1. https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html#selenium.webdriver.support.expected_conditions.visibility_of_element_located
  2. http://selenium-python.readthedocs.io/waits.html
Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
1

C#

You can use waiter to element to become visible like this:

var element = Waiter.Until(ExpectedConditions.ElementIsVisible(By.Id("ID"))).FirstOrDefault();
Mohsin Awan
  • 1,176
  • 2
  • 12
  • 29