为了账号安全,请及时绑定邮箱和手机立即绑定

如何点击 python selenium 中的按钮?

如何点击 python selenium 中的按钮?

元芳怎么了 2024-01-12 10:25:50
<div class="gift">    <button type="button" data-log-actionid-label="gift" data-log-body="{&quot;current_product_no&quot; : &quot;1003072802&quot;, &quot;group_product_no&quot; : &quot;&quot;, &quot;group_product_firstview_no&quot; : &quot;&quot;, &quot;product_price&quot; : &quot;5600&quot;, &quot;send_impression&quot;: &quot;Y&quot;}" data-is-send-log="true">        togift        <span class="ic_new">new</span>    </button></div>无法通过文本使用,因为此页面有很多文本togift我怎样才能点击这个按钮?
查看完整描述

3 回答

?
慕斯王

TA贡献1864条经验 获得超2个赞

没有足够的 html 源代码让我们知道如何编写正确且简短的元素位置声明。所以只需尝试以下操作xpath expressions:


"//div[@class='gift']/button[contains(text(),'togift') and ./span[contains(text(),'new')]]"

"//div[@class='gift']/button[contains(text(),'togift')]"

"//div[@class='gift']/button"


查看完整回答
反对 回复 2024-01-12
?
月关宝盒

TA贡献1772条经验 获得超5个赞

您可以尝试使用class name:


div = driver.find_element_by_class_name('gift')

btn = div.find_element_by_xpath('.//button')

btn.click()

如果有其他标签具有相同的类名,那么您可以使用css selector或full xpath


查看完整回答
反对 回复 2024-01-12
?
拉莫斯之舞

TA贡献1820条经验 获得超10个赞

应该点击 div 类礼物按钮。

driver.find_element_by_xpath("div[class='gift']/button").click()


查看完整回答
反对 回复 2024-01-12
  • 3 回答
  • 0 关注
  • 101 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信