python - 允许用户直接从 Python 中的 URL 中选择网页元素
问题描述
我一直在尝试找到一种方法来允许用户在 Python 中选择网页上的元素。
在下面的代码中,您可以看到我已经预定义了一个元素。我想要的是用户能够将鼠标悬停在网页上的元素上并单击它,然后将值返回给 python。非常类似于 chrome 中的元素选择器。
这可能吗?有任何想法吗
element = f"#pdp__select-size > li:nth-child({nth}) > button"
(ps 我已经导入 BeautifulSoup4 和 Selenium 来帮助抓取网页)
谢谢
金格
解决方案
要将鼠标悬停在元素上并单击它,您可以使用以下代码:
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
element = driver.find_element_by_css_selector('f"#pdp__select-size > li:nth-child({nth}) > button"')
actions.move_to_element(element).click().perform()
推荐阅读
- neo4j - 如何动态更改 neo4j-ogm 或 spring-data-neo4j 中的实体类型?
- javascript - 向正则表达式边界添加例外
- typescript - “(旗帜) ?” 句法。我不明白,也无法通过 Google 搜索找到它
- javascript - 无法使用 click() 函数单击锚标记
- java - 队列 jms [wso2] 消息代理。回拨服务
- c# - 过滤掉两个List的集合差
在 C# 中使用 LINQ - python - 不可变列表属性
- angular - 如何在 Angular 6 中为反应形式正确实现自定义正则表达式验证器?
- mysql - MySql 内部 JSON 插入或更新(如果存在)
- php - 如何将 Youtube iframe 转换为 amp-youtube 标签