I'm doing web scraping with Selenium in python and I need to click a button to extract some data this button is inside of an iframe which I already switched (switch.to_frame()) and Selenium still doesn't find by XPath
this is the code:
import pandas as pd
import numpy as np
import re
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
import requests
import os
import time
def ingresar_licitaciones():
resultado='3540-102-L123'
options = Options()
driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 10)
driver.get("https://www.mercadopublico.cl/Home")
driver.maximize_window()
inicio_sesion = driver.find_element("xpath","//button[@value]")
inicio_sesion.click()
clave_unica = driver.find_element("xpath","//img[@class]")
clave_unica.click()
usuario_run = driver.find_element("xpath","//input[@id='uname']")
usuario_claveunica = driver.find_element("xpath","//input[@id='pword']")
ingresar_run = "123456778-9"
ingresar_clave_unica = "password"
usuario_run.send_keys(ingresar_run)
usuario_claveunica.send_keys(ingresar_clave_unica)
ingresar = driver.find_element("xpath","//button")
ingresar.click()
entidad = wait.until(EC.visibility_of_element_located(('xpath','//span[@class="wrap-td"]')))
entidad.click()
ingresar2 = driver.find_element("xpath",'//a[@class="btn btn-pri"]')
ingresar2.click()
menu_licitaciones = driver.find_element("xpath",'//img[@alt="Expand Licitaciones "]')
acciones = ActionChains(driver)
acciones.move_to_element(menu_licitaciones).perform()
oferta_licitaciones = driver.find_element("xpath",'//a[@href="/BID/Modules/RFB/NEwSearchProcurement.aspx"]')
oferta_licitaciones.click()
driver.switch_to.frame("fraDetalle")
id_licitacion = resultado
ingresar_id_licitacion = driver.find_element("xpath",'//input[@id="txtProcCode"]')
ingresar_id_licitacion.send_keys(resultado)
boton_id_licitacion = driver.find_element("xpath",'//input[@id="btnSearchByCode"]')
boton_id_licitacion.click()
time.sleep(10)
ver_ficha = driver.find_element("xpath",'//input[@id="rptAcquisition_ctl01_itbnVerAdquisicion"]') # This is the problem, doesnt find the input
ver_ficha.click()
The error is in line 51 and says
NoSuchElementException Traceback (most recent call last)
Cell In[8], line 1
----> 1 ingresar_licitaciones()
File c:\Users\angel\Desktop\Licitacion\funciones_webscraper.py:155, in ingresar_licitaciones()
151 boton_id_licitacion.click()
153 time.sleep(10)
--> 155 ver_ficha = driver.find_element("xpath",'//input[@id="rptAcquisition_ctl01_itbnVerAdquisicion"]')
156 ver_ficha.click()
File c:\Users\angel\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py:740, in WebDriver.find_element(self, by, value)
737 by = By.CSS_SELECTOR
738 value = f'[name="{value}"]'
--> 740 return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
File c:\Users\angel\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py:346, in WebDriver.execute(self, driver_command, params)
344 response = self.command_executor.execute(driver_command, params)
345 if response:
--> 346 self.error_handler.check_response(response)
347 response["value"] = self._unwrap_value(response.get("value", None))
348 return response
File c:\Users\angel\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\errorhandler.py:245, in ErrorHandler.check_response(self, response)
243 alert_text = value["alert"].get("text")
244 raise exception_class(message, screen, stacktrace, alert_text) # type: ignore[call-arg] # mypy is not smart enough here
--> 245 raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@id="rptAcquisition_ctl01_itbnVerAdquisicion"]"}
(Session info: chrome=114.0.5735.201); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Stacktrace:
Backtrace:
GetHandleVerifier [0x00DAA813+48355]
(No symbol) [0x00D3C4B1]
(No symbol) [0x00C45358]
(No symbol) [0x00C709A5]
(No symbol) [0x00C70B3B]
(No symbol) [0x00C9E232]
(No symbol) [0x00C8A784]
(No symbol) [0x00C9C922]
(No symbol) [0x00C8A536]
(No symbol) [0x00C682DC]
(No symbol) [0x00C693DD]
GetHandleVerifier [0x0100AABD+2539405]
GetHandleVerifier [0x0104A78F+2800735]
GetHandleVerifier [0x0104456C+2775612]
GetHandleVerifier [0x00E351E0+616112]
(No symbol) [0x00D45F8C]
(No symbol) [0x00D42328]
(No symbol) [0x00D4240B]
(No symbol) [0x00D34FF7]
BaseThreadInitThunk [0x773800C9+25]
RtlGetAppContainerNamedObjectPath [0x77867B1E+286]
RtlGetAppContainerNamedObjectPath [0x77867AEE+238]
I would like help with the click of that button to keep going with my code and execute the function`
[1]: https://i.stack.imgur.com/yoKnO.png here is the image to the html code that i want to acces, i hope now is simplier my question