python - 无法使用 Python Selenium 在跨度类中定位元素
问题描述
我一直在努力在跨度类中找到一个元素。该元素是一个单选标记按钮。这是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
由于上面的类不是唯一的,我尝试了以下方法:
dropdown_menu = self.driver.find_element_by_css_selector('[for="searchType_2"] .radio-checkmark')
当我使用上面的 CSS 选择器进行检查和搜索时,它可以工作。它显示为 1 of 1。但是当我运行代码时,出现以下异常:
no such element: Unable to locate element: {"method":"css selector","selector":"[for="searchType_2"] .radio-checkmark"}
(Session info: chrome=92.0.4515.107
谢谢
解决方案
您尝试访问的元素位于 iframe 内。在访问其中的任何元素之前,您需要切换到 iframe
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();
推荐阅读
- oracle - ORA-00920: 无效的关系运算符,即使没有括号错误
- javascript - Vue meta 没有得到更新
- sql-server - 使用 Azure 逻辑应用在 Sharepoint Online 中插入缺失值
- unit-testing - 测试资源管理器无法发现 VS2019 中的任何单元测试
- python - cv2 中找不到detectMultiScale 函数?
- dialogflow-es - 卡片没有显示 Dialogflow 信使?
- c# - C# Selenium Chromedriver 只显示“数据”
- kotlin - Kotlin/Multiplatform 如何在控制台上查看测试的标准输出/标准错误?
- python - Django rest框架隐藏列逻辑
- xcode - 原生 arm64 和原生 Intel/rosetta2 x86_64 “返回后堆栈内存使用”的区别