python - Selenium:我找不到任何元素
问题描述
代码
<div class="dropButton" style="color: rgba(0, 0, 0, 0.87); background-color: transparent; transition: all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms; box-sizing: border-box; font-family: Roboto, sans-serif; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); box-shadow: rgba(0, 0, 0, 0.16) 0px 3px 10px, rgba(0, 0, 0, 0.23) 0px 3px 10px; border-radius: 50%; display: inline-block;"><button tabindex="0" type="button" style="border: 10px; box-sizing: border-box; display: inline-block; font-family: Roboto, sans-serif; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); cursor: pointer; text-decoration: none; margin: 0px; padding: 0px; outline: none; font-size: inherit; font-weight: inherit; position: relative; vertical-align: bottom; background-color: rgb(0, 188, 212); transition: all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms; height: 56px; width: 56px; overflow: hidden; border-radius: 50%; text-align: center;"><div><div style="transition: all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms; top: 0px;"><svg viewBox="0 0 24 24" style="display: inline-block; color: rgb(255, 255, 255); fill: rgb(255, 255, 255); height: 56px; width: 24px; user-select: none; transition: all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms; line-height: 56px;"><path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"></path></svg></div></div></button></div>
没有 iframe
我收到此错误
Unable to locate element
driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/header/div/button/div/div/svg').click()
解决方案
您的 xpath 不正确。使用.dropButton button
css 选择器并WebDriverWait
等待元素可点击。您可以在此处找到有关定位器策略的有用信息。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 15)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.dropButton button'))).click()
如果有更多带有选择器的按钮,请添加带有root
id 的元素:
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#root .dropButton button'))).click()
推荐阅读
- mysql - 具有 90% 相同列的两个不同表的最佳设计
- android - React Native iOS 构建失败,“多个命令产生”错误
- python - 使用 Python 读取位于 SharePoint 文件夹中的 Excel 工作表
- reactjs - reactjs中REF和INNERREF的区别
- javascript - 无法将 frida 函数附加到 android 本机 lib 函数
- python - 如何重新排序行,通过在 python 中使用 pandas 或 numpy 移动多个单独的行下面的 X 行
- javascript - 使用 setTimeout() 在 React 中渲染一个带有一些超时的列表
- php - 使用 php 和 curl 从 url 获取 JSON 数据
- intel-mkl - 使用 MKL 进行一维线性插值
- deep-learning - PyTorch,带有 LSTM 的 PPO 不使用情节轨迹进行训练