单击按钮后切换到动态iframe

2024-09-24 02:13:49 发布

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

我试图切换到一个新的框架,点击按钮后打开。不幸的是,我收到一个

driver = webdriver.Firefox()

driver.get('https://www.milanuncios.com/dacia-de-segunda-mano/dacia-sandero-1-5-dci-exportacion-323650137.htm')
time.sleep(5)

driver.find_element_by_xpath('//button[@id="pagAnuShowContactForm"]').click()
time.sleep(5)

在这里,它可以打开联系信息。在这里,我想在新打开的窗口中执行操作。 我尝试了以下选项:

1

contact = driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))

2

contact = driver.find_element_by_xpath('//div[@class="telefonos"]')

Tags: 框架getbytimedrivercontactsleepelement
1条回答
网友
1楼 · 发布于 2024-09-24 02:13:49

元素存在于iframe IDifrw中,您需要首先切换到iframe

诱导WebDriverWaitframe_to_be_available_and_switch_to_it()

诱导WebDriverWaitvisibility_of_element_located()

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

driver = webdriver.Firefox()
driver.get('https://www.milanuncios.com/dacia-de-segunda-mano/dacia-sandero-1-5-dci-exportacion-323650137.htm')
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[@id='pagAnuShowContactForm']"))).click()
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"ifrw")))
print(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.CSS_SELECTOR,".telefonos"))).text.strip())
driver.close()

输出

663473583

相关问题 更多 >