简体   繁体   English

如何找到使用python在Selenium中单击的按钮?

[英]how to find button to click in Selenium using python?

</div>
<fieldset class="pi-search-form__footer">
<div class="pi-search-form__footer-cta" data-ng-show="stay.isStandardBooking()">
<button class="btn btn--primary" data-ng-click="submitSearch()">
<span data-ng-switch="ctaText">
<span data-ng-switch-when="checkavailability">Check availability</span>
<span data-ng-switch-when="searchnow">Search now</span>
<span data-ng-switch-default data-pi-track-click="SEARCH_AGAIN">Search</span>
</span>
</button>
</div>

This is my button code and I want to click "Check availability" button. 这是我的按钮代码,我想单击“检查可用性”按钮。 I use that code but it not works. 我使用该代码,但它不起作用。

browser.find_elements_by_xpath(".//span[contains(text(), 'Check availability')]")

it should be 它应该是

browser.find_elements_by_xpath(".//span[contains(text(), 'Check availability')]")

and if it's not working use explicit wait. 如果不起作用,请使用显式等待。

from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 30)
checkAvailability = wait.until(EC.presence_of_element_located((By.XPATH, ".//span[contains(text(), 'Check availability')]")))

Here is the Answer to your Question: 这是您的问题的答案:

As you want to click on a button so your xpath should be unique and should identify a single element. 当您想单击一个按钮时,您的xpath应该是唯一的,并且应该标识一个元素。 Hence instead of find_elements_by_xpath you should consider using find_element_by_xpath . 因此,而不是find_elements_by_xpath你应该考虑使用find_element_by_xpath To click on the Check availability button you can use the following line of code: 要单击“ Check availability按钮,可以使用以下代码行:

//ensure the imports
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
//other code
//click on Check availability button
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn--primary']//span[@data-ng-switch-when='checkavailability']")))
browser.find_element_by_xpath("//button[@class='btn btn--primary']//span[@data-ng-switch-when='checkavailability']").click()

Let me know if this Answers your Question. 让我知道这是否回答了您的问题。

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

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