Carlos
Carlos

Reputation: 37

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

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

Upvotes: 1

Views: 84

Answers (1)

itronic1990
itronic1990

Reputation: 1441

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();

Upvotes: 1

Related Questions