python - 如何从 Selenium 和 Python 提供的 HTML 中找到元素?
问题描述
实际上这个按钮位于容器内。请提供python的语法
这里这个属性有唯一的名字,但是不知道怎么用[data-control-name="job_search_category_suggestion_it"]
<button class="jobs-search-category-suggestions__button button-secondary-medium-muted mb2 mr2" data-control-name="job_search_category_suggestion_it" data-ember-action="" data-ember-action-8126="8126">
<li-icon aria-hidden="true" type="search-icon" size="small"><svg viewBox="0 0 24 24" width="24px" height="24px" x="0" y="0" preserveAspectRatio="xMinYMin meet" class="artdeco-icon" focusable="false"><path d="M14,12.67L11.13,9.8A5,5,0,1,0,9.8,11.13L12.67,14ZM3.88,7A3.13,3.13,0,1,1,7,10.13,3.13,3.13,0,0,1,3.88,7Z" class="small-icon" style="fill-opacity: 1"></path></svg></li-icon>IT
</button>
我使用了这段代码:
elem = driver.find_elements_by_xpath('//button[@data-control-name="job_search_category_suggestion_it]').click()
解决方案
根据您共享的HTML,所需的元素是动态的,因此您需要诱导WebDriverWait使元素可点击,您可以使用以下解决方案:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='jobs-search-category-suggestions__button button-secondary-medium-muted mb2 mr2'][contains(.,'IT')]"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
推荐阅读
- javascript - 如何在 JavaScript 中与 json 交互?
- r - 如何将不同的行值组合成R中的单列
- python - VS Code 中的 Python:从子文件夹导入模块时出错
- python - 我该怎么做才能使我的约会时间变得可行?
- docker - 我可以使用 Hasura 快速入门进行生产吗?
- php - Docker:docker-compose 将文件从容器复制到主机
- android-studio - java.lang.IllegalStateException:在 android:onClick 的父或祖先上下文中找不到方法 onCreate_Clicked(View)
- python - Python - 使用异步工作人员池时如何处理 KeyboardInterrupt 并干净地退出?
- google-apps-script - 如何使用谷歌应用脚本复制特定表格并将其粘贴到文档的特定部分?
- r - 插入符号 CV 中的平均预测值