python - 如何通过类名找到 btn?

标签 python selenium selenium-webdriver

我有这个html代码: <button class="_2ic5v"><span aria-label="Like" class="glyphsSpriteComment_like u-__7"></span></button>

我试图通过 phyton 和 selenium webdriver 库找到满足此类的所有元素: likeBtn = driver.find_elements_by_class_name('_2ic5v')

但是当我打印时 likeBtn 它打印 []

我想找到与此 div/span 类或 aria-label 相关的所有按钮

我怎样才能成功做到这一点?提前致谢

更新 - 当我从页面复制 Xpath 时,打印保持不变

最佳答案

尝试以下 Xpath 选项这将找到包含 aria-label='Like' 的按钮。

//button[./span[@aria-label='Like' and starts-with(@class,'glyphsSpriteComment_like')]]

代码:

likeBtn=driver.find_elements_by_xpath("//button[./span[@aria-label='Like' and starts-with(@class,'glyphsSpriteComment_like')]]")
for button in likeBtn:
    button.click() 
<小时/>

已编辑:

引发WebDriverWait

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

likeBtn=WebDriverWait(driver,20).until(EC.presence_of_all_elements_located((By.XPATH,"//button[./span[@aria-label='Like' and starts-with(@class,'glyphsSpriteComment_like')]]")))

关于python - 如何通过类名找到 btn?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58382323/

相关文章:

python - 使用 matplotlib 绘图时如何避免 PyCharm 控制台崩溃 "WARNING: QApplication was not created in the main() thread"?

python - 以字母 "s"结尾的 ORM 类未正确生成

javascript - grunt exec 卡住,期待来自 django collectstatic 的 0 状态代码

Selenium 等待两个元素之一出现

python - 在笔记本中导入 Polars 会导致内核崩溃

selenium - 由于 FireBug 不再是 FF 56 的选项,如何检查 Selenium v​​3.6 的元素?

Python:Selenium "no such element"XPath 或 ID

c# - 查找元素是否确认页面已完全加载?

java - Google editboix 给出 InvalidElementStateException :Element is disabled and so may not be used for actions Command duration

javascript - 用 Protractor 打开文件