Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium Webdriver(Python)-无法在嵌套的iFrame中找到元素_Python_Selenium_Selenium Webdriver_Iframe_Css Selectors - Fatal编程技术网

Selenium Webdriver(Python)-无法在嵌套的iFrame中找到元素

Selenium Webdriver(Python)-无法在嵌套的iFrame中找到元素,python,selenium,selenium-webdriver,iframe,css-selectors,Python,Selenium,Selenium Webdriver,Iframe,Css Selectors,我试图从位于网页内的iframe中获取一些数据。网页的URL为。我正在尝试访问下图所示的按钮: 当我右键单击按钮(位于iframe内部)查看源代码时,我可以看到HTML id和名称(请参见下面的屏幕截图): 按钮的“id”为“新建客户按钮”。但是,当我使用selenium webdriver的driver.find\u element\u by\u id(“new\u customer\u button”)访问该按钮时,该代码无法在iframe内找到该按钮,并引发以下错误: NoSuchEl

我试图从位于网页内的iframe中获取一些数据。网页的URL为。我正在尝试访问下图所示的按钮:

当我右键单击按钮(位于iframe内部)查看源代码时,我可以看到HTML id和名称(请参见下面的屏幕截图):

按钮的“id”为“新建客户按钮”。但是,当我使用selenium webdriver的
driver.find\u element\u by\u id(“new\u customer\u button”)
访问该按钮时,该代码无法在iframe内找到该按钮,并引发以下错误:

NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"new_customer_button"}
以下是我迄今为止尝试过的代码:

from selenium import webdriver
chrome_path = r"C:\Users\gh455\Downloads\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://www.nissanoflithiasprings.com/schedule-service")

dest_iframe = driver.find_elements_by_tag_name('iframe')[0] 
driver.switch_to.frame(dest_iframe)

driver.find_element_by_id("new_customer_button")

不知道为什么会这样。任何帮助都将不胜感激。谢谢

元素位于多个
标记中,您需要逐个切换到它们。您还应该最大化窗口并使用显式等待,因为加载需要一些时间

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

chrome_path = r"C:\Users\gh455\Downloads\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.maximize_window()
driver.get("https://www.nissanoflithiasprings.com/schedule-service")

wait = WebDriverWait(driver, 10)

# first frame - by css selector
wait.until(ec.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, '[src^="https://consumer.xtime.com"]')))

# second frame - by ID
wait.until(ec.frame_to_be_available_and_switch_to_it('xt01'))

driver.find_element_by_id("new_customer_button")

元素位于多个
标记中,您需要逐个切换到它们。您还应该最大化窗口并使用显式等待,因为加载需要一些时间

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

chrome_path = r"C:\Users\gh455\Downloads\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.maximize_window()
driver.get("https://www.nissanoflithiasprings.com/schedule-service")

wait = WebDriverWait(driver, 10)

# first frame - by css selector
wait.until(ec.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, '[src^="https://consumer.xtime.com"]')))

# second frame - by ID
wait.until(ec.frame_to_be_available_and_switch_to_it('xt01'))

driver.find_element_by_id("new_customer_button")
若要
,请在文本为Make·Year·Model的元素上单击()
,因为所需元素位于嵌套的
中,因此您必须:

  • 诱导WebDriverWait以使所需父帧可用并切换到该帧
  • 诱导WebDriverWait以使所需的子帧可用,并切换到该子帧
  • 将所需
    元素的WebDriverWait设置为可单击()
  • 您可以使用以下解决方案:

    • 代码块:

      from selenium import webdriver
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
      chrome_options = webdriver.ChromeOptions() 
      chrome_options.add_argument("start-maximized")
      chrome_options.add_argument('disable-infobars')
      driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\WebDrivers\chromedriver.exe')
      driver.get("https://www.nissanoflithiasprings.com/schedule-service")
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src*='com/scheduling']")))
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src*='consumerportal']")))
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.button.button--action.btn.btn-secondary#new_customer_button"))).click()
      
  • 浏览器快照:

在这里,您可以找到关于

若要
,请在文本为Make·Year·Model的元素上单击()
,因为所需元素位于嵌套的
中,因此您必须:

  • 诱导WebDriverWait以使所需父帧可用并切换到该帧
  • 诱导WebDriverWait以使所需的子帧可用,并切换到该子帧
  • 将所需
    元素的WebDriverWait设置为可单击()
  • 您可以使用以下解决方案:

    • 代码块:

      from selenium import webdriver
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
      chrome_options = webdriver.ChromeOptions() 
      chrome_options.add_argument("start-maximized")
      chrome_options.add_argument('disable-infobars')
      driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\WebDrivers\chromedriver.exe')
      driver.get("https://www.nissanoflithiasprings.com/schedule-service")
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src*='com/scheduling']")))
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src*='consumerportal']")))
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.button.button--action.btn.btn-secondary#new_customer_button"))).click()
      
  • 浏览器快照:

在这里,您可以找到关于