【问题标题】:Not able to click on a button in selenium python BDD framework无法单击 selenium python BDD 框架中的按钮
【发布时间】:2022-01-23 14:41:21
【问题描述】:

我无法在 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()

提前致谢。

【问题讨论】:

    标签: python selenium xpath css-selectors webdriverwait


    【解决方案1】:

    要点击一个可点击元素,您需要为element_to_be_clickable()诱导WebDriverWait,您可以使用以下Locator Strategies之一:

    • 使用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
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      相关资源
      最近更新 更多