无法点击硒python BDD框架中的按钮



我无法在BDD框架中单击Selenium网络驱动程序上的按钮(保存(。

我可以点击通用脚本,但当我通过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()

提前谢谢。

要点击可点击元素,您需要诱导WebDriver等待元素_To_be_clickle((,然后您可以使用以下定位器策略之一:

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

最新更新