![](/img/trans.png)
[英]How to find and click on a button in page by name using Selenium and Python
[英]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>
這是我的按鈕代碼,我想單擊“檢查可用性”按鈕。 我使用該代碼,但它不起作用。
browser.find_elements_by_xpath(".//span[contains(text(), 'Check availability')]")
它應該是
browser.find_elements_by_xpath(".//span[contains(text(), 'Check availability')]")
如果不起作用,請使用顯式等待。
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')]")))
這是您的問題的答案:
當您想單擊一個按鈕時,您的xpath
應該是唯一的,並且應該標識一個元素。 因此,而不是find_elements_by_xpath
你應該考慮使用find_element_by_xpath
。 要單擊“ 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()
讓我知道這是否回答了您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.