【问题标题】:Element not Interactable Error when trying to automate a process尝试自动化流程时出现元素不可交互错误
【发布时间】:2022-01-15 05:04:20
【问题描述】:

我正在尝试发送一个值并单击按钮以检索某些 pdf,但我收到一个错误,即元素不可交互。我尝试使用不同的选择器,但我得到了同样的错误。 以下是我的代码:

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

from selenium.webdriver.chrome.options import Options
options=Options()
prefs = {"download.default_directory" : "D:\Trvael_Test_Script\GSTInvoiceDownloads"}
options.add_experimental_option("prefs",prefs)

from selenium.webdriver.chrome.service import Service
s=Service('C:/webdrivers/chromedriver.exe')

driver = webdriver.Chrome(service=s,options=options)
driver.get("link")

time.sleep(10)

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="GST"]/span[1]'))).click()

time.sleep(5)

driver.switch_to.frame(driver.find_element(By.ID,'ifgstdownloadnFrame'))

time.sleep(5)


driver.find_element(By.XPATH,'//*[@id="txtpnr"]').send_keys('pnr')

driver.find_element(By.XPATH,'//*[@id="btnSubmit"]').click()```

Can anyone please let me know how to make the element interactable?

【问题讨论】:

    标签: python selenium selenium-webdriver automation


    【解决方案1】:

    要将字符序列发送到 PNR 字段,您可以使用以下Locator Strategy

    • 使用CSS_SELECTOR

      driver.find_element(By.CSS_SELECTOR, "input#txtpnr").send_keys("AGQR8U")
      

    理想情况下,发送您需要诱导WebDriverWaitelement_to_be_clickable() 的文本,您可以使用以下Locator Strategy

    • 使用CSS_SELECTOR

      driver.get("https://book.spicejet.com/")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='GST Invoice']"))).click()
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"div#GSTInvoicePopUP iframe#ifgstdownloadnFrame")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#txtpnr"))).send_keys("AGQR8U")
      
    • 注意:您必须添加以下导入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
    • 浏览器快照:

    【讨论】:

    • 我可以点击 GST Invoice,但我无法将 pnr 值发送到之后出现的弹出窗口。我需要将 PNR 值发送到那个给出元素不可交互错误的框。
    • 查看更新的答案并让我知道状态。
    • 成功了。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    相关资源
    最近更新 更多