如何使用Selenium和Python处理嵌套的iframe



我试图自动化一项日常任务,但遇到了一些嵌套的iframe。脚本运行得很好,直到取消选择所有文档类型的部分失败。

我试着等到元素可以点击。此外,我尝试使用id、css_selector和XPath访问元素,但没有任何帮助。

我认为元素位于嵌套的iframe中,但无法获得正确的序列。

这是我的脚本:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
options = webdriver.ChromeOptions()
options.add_argument("disable-infobars")
#options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument('--disable-dev-shm-usage')
#options.add_argument('--disable-gpu')
options.add_argument("--start-maximized")
options.add_experimental_option("excludeSwitches",["ignore-certificate-errors"])
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
link = r"https://countyfusion10.kofiletech.us/countyweb/loginDisplay.action?countyname=TylerWV"
options.add_experimental_option("prefs", {
"download.directory_upgrade":True,
"safebrowsing.enabled":True,
"download.prompt_for_download":False,
"plugins.always_open_pdf_externally": True,
})
driver = webdriver.Chrome(executable_path='./chromedriver', options=options)
driver.get(link)
#Login as Guest
driver.find_element_by_xpath(r'//*[@id="maindiv"]/table[2]/tbody/tr[1]/td[2]/table/tbody/tr/td/center/input').click()
time.sleep(5)
#Accept Disclaimer
driver.switch_to.frame("bodyframe")
driver.find_element_by_id(r'accept').click()
time.sleep(30)
#Deselect All types
driver.switch_to.default_content()
boxes = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,r'//*[@id="elemAllNamesJSP_20"]/table/tbody/tr/td[2]/span/input[1]')))
print(boxes)

我不确定你是否必须切换到默认内容,因为你离开bodyframeiframe访问";"盒子";元素,必须先切换到第一个父iframe,然后切换到内部父项,再切换到内部子项。像这样:

driver = webdriver.Chrome(executable_path='./chromedriver', options=options)
driver.get(link)
#Login as Guest
driver.find_element_by_xpath(r'//*[@id="maindiv"]/table[2]/tbody/tr[1]/td[2]/table/tbody/tr/td/center/input').click()
#Accept Disclaimer
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it(By.NAME,"bodyframe"))
driver.find_element_by_id(r'accept').click()
#Deselect All types
driver.switch_to.default_content()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it(By.NAME,"bodyframe"))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it(By.NAME,"dynSearchFrame"))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it(By.NAME,"criteriaframe"))
boxes = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,r'//*[@id="elemAllNamesJSP_20"]/table/tbody/tr/td[2]/span/input[1]')))
print(boxes)

我不确定你试图通过打印web元素来实现什么;"盒子";使用print(boxes)
我还删除了硬编码的延迟。尤其是30秒的睡眠,你不需要它。使用网络驱动程序显式等待
它将工作得更快,而且仍然稳定。

相关内容

  • 没有找到相关文章