我有一个程序,可以使用我输入的特定信息在PLM平台ENOVIA上自动进行数据输入。我正在使用Selenium来浏览网页。在一段中,程序必须输入4个嵌套框架才能单击某个元素。它做得很好并且进行了必要的编辑,我面临的问题是当我尝试退出框架回到HTML代码顶部以使用 switch_to.default_content()返回首页时。出于某种原因,在切换到嵌套框架的第4个子框架并进行了这些编辑后,我看到窗口更改为仅包含这些框架及其内部内容,而第一个父框架上方没有任何内容。因此 switch_to.default_content()似乎无效。
这是真正重要的代码,它将切换到第四个也是最后一个帧“ DSCObjectSummary”并进行这些编辑
def clickCheck(Method, Locator, elemName):
#Add If Else to check for element availibility
#Do Retries of searchess
try:
wait.until(EC.element_to_be_clickable((Method, Locator)))
print(elemName + ' Clickable')
except NoSuchElementException:
print("Cannot Find Element")
...... Lots of code and comes to this point
#Switching into 4 nested frames to click "EDIT DETAILS" Button
wait.until(EC.frame_to_be_available_and_switch_to_it("content"))
wait.until(EC.frame_to_be_available_and_switch_to_it("detailsDisplay"))
wait.until(EC.frame_to_be_available_and_switch_to_it("portalDisplay"))
wait.until(EC.frame_to_be_available_and_switch_to_it("DSCObjectSummary"))
clickCheck(By.ID, 'DSCEditObjectSummary', "Edit Details")
elemEdit = browser.find_element_by_id("DSCEditObjectSummary")
elemEdit.click()
#Click the Customer Dropdown
clickCheck(By.ID, "PMCCustomerId", "Customer Dropdown")
elemCust = browser.find_element_by_id("PMCCustomerId")
elemCust.click()
elemCust.send_keys("Ces")
"""Add If Conditions to check part number"""
#Click Document Type Field
clickCheck(By.ID, "PMCDocumentTypeId", "Document Type")
elemDoc = browser.find_element_by_id("PMCDocumentTypeId")
elemDoc.click()
elemDoc.send_keys("MOD")
"""Add Check to see if part is Active/Obsol, if Obs, make Active"""
#Click Dashnumber field
clickCheck(By.ID, "PMCDashNumber", "Dash Number")
elemDash = browser.find_element_by_id("PMCDashNumber")
elemDash.click()
elemDash.clear()
elemDash.send_keys("-1")
#Click Done Button
clickCheck(By.CSS_SELECTOR, "button[class='btn-primary'][type='button']", "Done Button")
elemDone = browser.find_element_by_css_selector("button[class='btn-primary'][type='button']")
elemDone.click()
browser.switch_to.default_content()
time.sleep(20)
我尝试依次使用 switch_to.frame(framename)从第4个父帧向后切换到第一个父帧,然后仅使用 switch_to.default_content()引发NoSuchFrameException 。我想要的是能够像回到框架一样退出框架,而无需更改/删除窗口中的任何内容并回到顶层HTML。
答案 0 :(得分:0)
作为driver.switch_to.default_content()
的替代方法,您还可以使用:
browser.switch_to.parent_frame()
由于您仍然面临相同的问题,因此更好的方法是等待elemDone.click()
步骤完成( JavaScript / Ajax调用)并且您可以为某些visibility_of_element_located()
诱导 WebDriverWait ,如下所示:
elemDone = browser.find_element_by_css_selector("button[class='btn-primary'][type='button']")
elemDone.click()
WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "css_of_an_element_which_gets_displayed_after_the_click")))
browser.switch_to.default_content()
# as an alternative use `switch_to.parent_frame()`
# browser.switch_to.parent_frame()
注意:出于调试目的,您可以将 WebDriverWait 替换为time.sleep(5)
“Malformed URL: can't access dead object” in selenium when trying to open google
答案 1 :(得分:0)
尝试查找所有iframe,然后切换到所需的iframe。
// find all your iframes
List<WebElement> iframes = driver.findElements(By.xpath("//iframe"));
// print your number of frames
System.out.println(iframes.size());
// you can reach each frame on your site
for (WebElement iframe : iframes) {
// switch to every frame
driver.switchTo().frame(iframe);
// now within the frame you can navigate like you are used to
}