簡體   English   中英

Python Selenium Webdriver找不到元素

[英]Python selenium webdriver couldn't find element

我的python Selenium程序找不到我認為網絡元素所在的框架。 如果我做錯了事,請告訴我。 謝謝。


# switch back to main window
driver.switch_to.window(main_window_handle)
time.sleep(5)
driver.switch_to.frame("header");
driver.find_element_by_id("a_1").click()
time.sleep(10)

# switch back to main window
driver.switch_to.window(main_window_handle)
time.sleep(5)
driver.switch_to.frame("contents");               ***<----- failed here***
time.sleep(5)
driver.switch_to.window("itreecontrol");
time.sleep(5)
# Click on desired item
driver.find_element_by_xpath("//div/a/span/span/span").click()

在此處輸入圖片說明

==============================

附加問題:在彈出的窗口中找不到元素

# switch back to main window
driver.switch_to.window(main_window_handle)
time.sleep(5)
driver.switch_to.frame("header");
driver.find_element_by_id("a_1").click()
time.sleep(10)

# Go to correct frame first
driver.switch_to.default_content()
driver.switch_to.frame("contents");
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"));
time.sleep(5)
# Click on plus icon to pop up a window
driver.find_element_by_xpath("//div/a/span/span/span").click()

# Enter your info in this text-field element in the popped-up window
# (failed to find "unitName-inputEl" here)
driver.find_element_by_id("unitName-inputEl").clear()       <--- failed here

更新代碼-1

# switch back to main window
driver.switch_to.window(main_window_handle)   
# Click on 'Firewall' panel
time.sleep(5)
driver.switch_to.frame("header");
driver.find_element_by_id("a_1").click()
time.sleep(10)

# switch to default content
driver.switch_to.default_content()
# Go to the correct frame first
driver.switch_to.frame("contents");
driver.switch_to.frame("itreecontrol");
time.sleep(5)
# Click on plus-box icon to display pop-up
driver.find_element_by_xpath("//div/a/span/span/span").click()

driver.find_element_by_id("unitName-inputEl").clear()
#driver.find_element_by_id("unitName-inputEl").send_keys("tz400")
wait(driver, 10).until(EC.element_to_be_clickable((By.ID, "unitName-inputEl"))).send_keys('tz400')

在此處輸入圖片說明

在此處輸入圖片說明

在此處輸入圖片說明

切換到<iframe id="header">您需要切換回默認內容才能切換到<iframe id="contents">

嘗試:

driver.switch_to.frame("header");
driver.find_element_by_id("a_1").click()
time.sleep(10)

driver.switch_to.default_content()
time.sleep(5)
driver.switch_to.frame("contents")
driver.switch_to.frame("itreecontrol")

要處理input字段,您可以嘗試使用ExplicitWait ,如下所示:

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

wait(driver, 10).until(EC.element_to_be_clickable((By.ID, "unitName-inputEl"))).send_keys('tz4‌​00')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM