Vipul Gangwar Vipul Gangwar - 3 years ago 159
Python Question

how to find button to click in Selenium using python?

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

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')]")

Answer Source

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. Hence instead of find_elements_by_xpath you should consider using find_element_by_xpath. To click on the Check availability button you can use the following line of code:

//ensure the imports
from import By
from import WebDriverWait
from 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download