簡體   English   中英

無法單擊 selenium python BDD 框架中的按鈕

[英]Not able to click on a button in selenium python BDD framework

我無法在 BDD 框架中單擊 Selenium webdriver 上的按鈕(保存)。

我可以單擊一般腳本,但是當我通過 BDD 框架執行相同的腳本時它不起作用,請幫助我。

<button data-id="save-button" aria-label="Save" type="button" class="inline-flex items-center font-bold border rounded transition duration-300 ease-out hover:bg-primary-700 active:bg-primary-800 bg-primary-600 button-normal text-white justify-center text-base border-primary-600 px-4" style="background-image: linear-gradient(rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0));">Save</button>

我嘗試過以下代碼:

element = self.driver.find_element(By.XPATH,"/html/body/div[1]/div/div/div/div[1]/div/div[2]/div[2]/button[2]")
        self.driver.execute_script("arguments[0].click();", element)

element = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable(By.XPATH, "//button[text()='Clear all changes']/following::button[@data-id='save-button']")).click()
        self.driver.execute_script("arguments[0].click();", element)

self.driver.find_element_by_xpath("/html/body/div[1]/div/div/div/div[1]/div/div[2]/div[2]/button[2]").click()

actions.click(self.driver.find_element_by_xpath("/html/body/div[1]/div/div/div/div[1]/div/div[2]/div[2]/button[2]")).perform()
        actions.move_to_element(button).click(button).perform()

self.driver.find_element_by_class_name('inline-flex items-center font-bold border rounded transition duration-300 ease-out hover:bg-primary-700 active:bg-primary-800 bg-primary-600 button-normal text-white justify-center text-base border-primary-600 px-4').click()

ele =self.driver.find_element_by_css_selector("button[data-id='save-button']").click()
        ele.click()

我在這個按鈕上浪費了兩天多。 相同的元素適用於正常的腳本執行,而不使用任何框架。

下面的工作腳本:

self.driver.find_element_by_xpath("/html/body/div[1]/div/div/div/div[1]/div/div[2]/div[2]/button[2]").click()

提前致謝。

要單擊可點擊元素,您需要為element_to_be_clickable()誘導WebDriverWait ,並且您可以使用以下任一定位器策略

  • 使用CSS_SELECTOR

     WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-id='save-button'][aria-label='Save']"))).click()
  • 使用XPATH

     WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@data-id='save-button' and @aria-label='Save'][text()='Save']"))).click()
  • 注意:您必須添加以下導入:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM