无法找到硒中 flex 下存在的元素

Unable to locate elements present under flex in selenium

我正在尝试自动化 Demo Website link。但是无法使用我的相对路径 //a[contains(text(),'Shop Now')][=20= 找到 Shop Now 按钮].我注意到 flex 是写在 DOM 这会影响它吗?

谁能告诉我我在这里遗漏了什么?

Shop Now Button Image

尝试以下方法定位并点击

button= driver.execute_script('retun document.querySelector("#header-promo > div > div > div.content > div.button > a")')
button.click();

元素 立即购买 中,因此您必须:

  • 诱导 WebDriverWait 所需的 帧可用并切换到它.

  • 诱导 所需的 元素可点击

  • 您可以使用以下任一项:

    • 使用LINK_TEXT:

      driver.get("https://demo.competethemes.com/")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#iframe")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Shop Now"))).click()
      
    • 使用 XPATH:

      driver.get("https://demo.competethemes.com/")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='iframe']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Shop Now']"))).click()
      
  • 注意:您必须添加以下导入:

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