简体   繁体   中英

Python selenium webdriver couldn't find element

My python Selenium program could not find the frame in which I think the web-element is residing. Please let me know if I am doing something wrong. Thank you.


# 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()

在此处输入图片说明

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

Additional issue: Cannot find element on popped-up window

# 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

Updated code -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')

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

After switching to <iframe id="header"> you need to switch back to default content to be able to switch to <iframe id="contents">

try:

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")

To handle input field you can try to use ExplicitWait as below:

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM