简体   繁体   English

如何在python selenium中单击按钮?

[英]how to click button in python selenium?

<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>

Can't use by text, because this page have lots of text togift不能通过文字使用,因为这个页面有很多文字togift

How can I click this button ?我怎样才能点击这个按钮?

You can try using the class name :您可以尝试使用class name

div = driver.find_element_by_class_name('gift')
btn = div.find_element_by_xpath('.//button')
btn.click()

If there are other tags with the same class name, then you can use the css selector or the full xpath如果还有其他标签具有相同的类名,那么您可以使用css selectorfull xpath

应该点击带有按钮的 div 类礼物。

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

Not enough html source to let us know how to write a correct and short element's location statement.没有足够的 html 源代码让我们知道如何编写正确且简短的元素位置声明。 So just try the below xpath expressions :所以只需尝试以下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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM