0

I use VSCode and Python 3.11.4 64-bit.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.action_chains import ActionChains

from datetime import datetime, timedelta
import csv
import xlrd
import math
import time
import warnings

edge_options = Options()
edge_options.add_experimental_option("detach", True)
#edge_options.add_argument("--start-maximized")
warnings.filterwarnings("ignore")

driver=webdriver.Edge(options=edge_options)
driver.implicitly_wait(10)
actions = ActionChains(driver)


#start browse
print("Strat browsing - ##############################################################")
driver.get("https://welcome.com.intraorange/")
nbolink = driver.find_element(By.XPATH, "/html/body/div[2]/div/div[4]/div[2]/div/div/ul/li[5]/a/span").click()

'changement onglet' 
chd = driver.window_handles[1]
driver.switch_to.window(chd)

'login active'
Activeboutton = driver.find_element(By.XPATH, "//*[@id='spanLinkActiver']").click()

'Mouseover Projets pour click sur recherche globale '
proj = driver.find_element(By.XPATH, "//*[@id='main_menu']/ul/li[3]")
actions.move_to_element(proj).perform()

rglobale = driver.find_element(By.XPATH,"//*[@id='main_menu']/ul/li[3]/div/ul[4]/li[2]/a").click()

print("switch to default content - ##############################################################")
driver.switch_to.default_content()

print("switch to iframe - ##############################################################")

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it("body"))
#iframe = driver.find_element(By.TAG_NAME, "iframe")
#ifrm = driver.find_element(By.XPATH, "/html/body/div[2]/iframe")
#driver.switch_to.frame(iframe)

print("clear content ")
codop = driver.find_element(By.XPATH, "//*[@id='input_ope_code']").send_keys("12345")

#ok = driver.find_element(By.XPATH, "//*[@id='gs_search']").click()

print("end")
  • This is the result after run the code: Traceback (most recent call last): File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1264.0_x64__qbz5n2kfra8p0\Lib\runpy.py", line 198, in _run_module_as_main return _run_code(code, main_globals, None, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1264.0_x64__qbz5n2kfra8p0\Lib\runpy.py", line 88, in _run_code exec(code, run_globals) File "c:\Users\dbhc5570\.vscode\extensions\ms-python.python-2023.10.1\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\__main__.py", line 39, in <module> cli.main() File "c:\Users\dbhc5570\.vscode\extensions\ms-python.python-2023.10.1\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 430, in main run() File "c:\Users\dbhc5570\.vscode\extensions\ms-python.python-2023.10.1\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 284, in run_file runpy.run_path(target, run_name="__main__") File "c:\Users\dbhc5570\.vscode\extensions\ms-python.python-2023.10.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path return _run_module_code(code, init_globals, run_name, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users\dbhc5570\.vscode\extensions\ms-python.python-2023.10.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code _run_code(code, mod_globals, init_globals, File "c:\Users\dbhc5570\.vscode\extensions\ms-python.python-2023.10.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code exec(code, run_globals) File "c:\Users\dbhc5570\Documents\_Sujets\Python-scraper-version doc\Vdocscrap.py", line 49, in <module> WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it("body")) File "C:\Users\dbhc5570\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\support\wait.py", line 86, in until value = method(self._driver) ^^^^^^^^^^^^^^^^^^^^ File "C:\Users\dbhc5570\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\support\expected_conditions.py", line 298, in _predicate driver.switch_to.frame(locator) File "C:\Users\dbhc5570\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\remote\switch_to.py", line 92, in frame self._driver.execute(Command.SWITCH_TO_FRAME, {"id": frame_reference}) File "C:\Users\dbhc5570\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\remote\webdriver.py", line 346, in execute self.error_handler.check_response(response) File "C:\Users\dbhc5570\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.JavascriptException: Message: javascript error: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.

I tried several methods but none of it alow me to switch and manipulate the elements inners to iframe.

2 Answers2

0

Change the below line:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it("body"))

TO:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "iframe_body")))

And once you finish actions within IFRAME, to come back to main content, you can use below code:

driver.switch_to.default_content()
Shawn
  • 4,064
  • 2
  • 11
  • 23
0

Given the HTML:

iframe

The desired element is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • You can use either of the following locator strategies:

    • Using ID:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"iframe_body")))
      
    • Using NAME:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"body")))
      
    • Using CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#iframe_body")))
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@name='body']")))
      
  • Note : You have to add the following imports :

     from selenium.webdriver.support.ui import WebDriverWait
     from selenium.webdriver.common.by import By
     from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352