Extract specific elements from a table with Selenium in Python

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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)
</code>
<code>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) </code>
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.

New contributor

Gaurav Dadlani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật