我有这个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/