I am trying to extract specific data from a table displayed on the web server of a device.
I am running into an error while running the code that I cannot seem to understand.
I believe the error is coming when trying to find the specific element on the table column which is an invalid expression.
Any help on the issue will be greatly appreciated.
Thank you.
Regards
import selenium.webdriver as webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
##user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0'
##edge_driver_path = os.path.join(os.getcwd(), 'msedgedriver.exe')
edge_driver_path = "msedgedriver.exe"
edge_service = Service(edge_driver_path)
edge_options = Options()
edge_options.add_experimental_option('excludeSwitches', ['enable-logging'])
edge_options.add_experimental_option("detach", True)
service = webdriver.EdgeService(log_output="C:/Users/Desktop/WebScraper/tutorial")
browser = webdriver.Edge(service=edge_service, options=edge_options)
browser.get('ipaddress')
WebDriverWait(browser, 60).until(EC.presence_of_element_located((By.ID, "ANALOGINPUTSR0C2")))
comp_table = browser.find_element(By.XPATH,"//table[@id='ANALOGINPUTSbody']")
comp_table_row = comp_table.find_element(By.XPATH,"//tr[contains(@class,'row')]")
comp_table_element = comp_table_row.find_element(By.XPATH,"//td(@id='ANALOGINPUTSR0C2')")
print(comp_table_element.text)
The error is shown below
File “c:UsersDesktopWebScrapertutorialtutorial.py”, line 29, in
comp_table_element = comp_table_row.find_element(By.XPATH,”//td(@id=’ANALOGINPUTSR0C2′)”)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersDesktopWebScrapertutorial.venvLibsite-packagesseleniumwebdriverremotewebelement.py”, line 417, in find_element
return self._execute(Command.FIND_CHILD_ELEMENT, {“using”: by, “value”: value})[“value”]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersDesktopWebScrapertutorial.venvLibsite-packagesseleniumwebdriverremotewebelement.py”, line 395, in _execute
return self._parent.execute(command, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersDesktopWebScrapertutorial.venvLibsite-packagesseleniumwebdriverremotewebdriver.py”, line 347, in execute
self.error_handler.check_response(response)
File “C:UsersDesktopWebScrapertutorial.venvLibsite-packagesseleniumwebdriverremoteerrorhandler.py”, line 229, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.JavascriptException: Message: javascript error: {“status”:32,”value”:”Unable to locate an element with the xpath expression //td(@id=’ANALOGINPUTSR0C2′) because of the following error:nSyntaxError: Failed to execute ‘evaluate’ on ‘Document’: The string ‘//td(@id=’ANALOGINPUTSR0C2′)’ is not a valid XPath expression.”}
(Session info: MicrosoftEdge=125.0.2535.51)
I tried different XPath expressions but this is the one that has narrowed down the problem for me.
Gaurav Dadlani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.