是否有一种方法可以使用python使用selenium在组合框“Region”中选择一个项目?

2024-10-03 19:21:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在这个网站LINKPython+Selenium中用这个脚本选择组合框“Region”的第一项

enter image description here

chromeOptions = webdriver.ChromeOptions()
prefs = {'profile.managed_default_content_settings.images':2, 'disk-cache-size': 4096 }
chromeOptions.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chromeOptions)

url = 'https://www.tgr.cl/certificado-pago-deudas-contribuciones-tramite/'
driver.get(url)
driver.switch_to.frame(driver.find_element_by_name('busqueda'))

driver.find_element_by_xpath("//select[@name='region']/option[text()='REGION DE ANTOFAGASTA']").click()

但是我在切换帧时遇到了这个错误。输出:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="busqueda"]"}

你知道为什么会这样吗


Tags: tonameurlby网站driverelementfind
2条回答

所有子文档,如<iframe>,您的元素在lazyload中的存在速度都比整个网页慢。最好等待几秒钟,或者等到Selenium检测到<iframe>

# Wait specifically for that iframe, then switch to it
from selenium.webdriver.support.ui import WebDriverWait

iframe_detector = WebDriverWait(driver, 5).until(lambda x: x.find_element_by_name('busqueda'),
                                                  message="Iframe never loaded!")
driver.switch_to.frame(iframe_detector)

#Just wait and hope it loads in
driver.implicitly_wait(5)
driver.switch_to.frame(driver.find_element_by_name('busqueda'))

您可能需要更长的等待时间来说明您的internet连接

问题是组合框是iframe的iframe。所以你需要切换到第一个iframe,然后再切换到他的孩子,这样你就没事了

相关问题 更多 >