簡體   English   中英

使用 Selenium 和 Python 在 iframe 中單擊元素時出現問題

[英]Issue with clicking an element within iframe using Selenium and Python

我正在使用下面的代碼來復制下面顯示的操作。 該腳本能夠 select 具有文本“操作員維護”的字段並使用操作鏈單擊它,但是,它只會隨機轉到具有員工 ID 字段的相應下一頁。

ieOptions = webdriver.IeOptions()
ieOptions.add_additional_option("ie.edgechromium", True)
ieOptions.add_additional_option("ie.edgepath",'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe')
ieOptions.ignore_zoom_level = True
driver = webdriver.Ie(executable_path='C:\Program Files (x86)\IEDriverServer.exe', options=ieOptions)

for key in parks:
    if PK == key:
        driver.get(parks[key][0])
        PW = parks[key][1]

# switch to selected iframe
driver.switch_to.frame('MWFApplicationFrame')
# Now continue with login process
UserName = driver.find_element(By.ID, "UserName")
UserName.send_keys(ID)
PassWord = driver.find_element(By.ID, "Password")
PassWord.send_keys(PW)
login = driver.find_element(By.ID, "Login")
login.click()
# switch to selected iframe
driver.switch_to.default_content()
emp = driver.find_element(By.ID, "menu").click()
driver.switch_to.frame('MWFPopupMenuFrame')
hoverable = driver.find_element(By.CLASS_NAME, "popupMenuCell")
ActionChains(driver)\
        .move_to_element(hoverable)\
        .click(hoverable)\
            .perform()

手動流程:

在此處輸入圖像描述

腳本的實際問題:

在此處輸入圖像描述

所示網頁的 Html:

在此處輸入圖像描述

在此處輸入圖像描述

由於所需元素位於<iframe>內,因此您必須:

  • 誘導WebDriverWait使所需的幀可用並切換到它

  • 誘導WebDriverWait使所需元素成為可點擊的。

  • 您可以使用以下任一定位器策略

    • 使用CSS_SELECTOR

       WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.popupMenuFrame#MWFPopupMenuFrame"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "table.popupMenuMainTable table.popupMenuSubTable tr[screenName='Operator Maintenance'] > td"))).click()
    • 使用XPATH

       WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='popupMenuFrame' and @id='MWFPopupMenuFrame']"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//table[@class='popupMenuMainTable']//table[@class='popupMenuSubTable']//tr[@screenName='Operator Maintenance']/td"))).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