Unable to locate an element in a span class using Python Selenium

Issue

I have been struggling to locate an element in a span class. The element is a radio-checkmark button. Here is the html:

<span class="radio-container" for="searchType_2">
<input class="form-check-input" type="radio" name="searchType" id="searchType_2" value="cidade">
<span class="radio-checkmark">
::after

As the classes above are not unique, I tried the following:

dropdown_menu = self.driver.find_element_by_css_selector('[for="searchType_2"] .radio-checkmark')

When I do the inspection and search using the CSS selector above it works. It shows me as 1 of 1. But when I run the code, I get the following exception:

no such element: Unable to locate element: {"method":"css selector","selector":"[for="searchType_2"] .radio-checkmark"}
(Session info: chrome=92.0.4515.107

Thanks

Solution

The element you are trying to access is inside an iframe. You need to switch to the iframe before accessing any element inside it

driver.switch_to_frame(driver.find_element_by_xpath("//iframe[@class='cz-map-frame']"))
driver.find_element_by_xpath("//input[@id='searchType_2']//following::span[@class='radio-checkmark'][1]").click();

Answered By – itronic1990

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published