This is a tough one for me, I’ve read through all earlier posts before posting the question.
HTML web table structure is as below, I’d like the date part from the span tag, below are all the code I’ve tried.
<td style="padding:2px 2px 2px 5px;width:25%;white-space:nowrap;text-align:left;">
<span tabindex="0">11/29/2023 04:01:10</span>
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
#from selenium.webdriver.support.ui import WebDriverWait
#from selenium.webdriver.support import expected_conditions as EC
#import pandas as pd
import time
DRIVER_PATH = 'C:Program Files (x86)Selenium ClientSelenium Driverschromedriver.exe'
service = Service(executable_path=DRIVER_PATH)
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
URL = 'https://www.test.com'
driver.implicitly_wait(2)
driver.get(URL)
#print(driver.page_source)
time.sleep(10)
print(driver.find_element(By.CSS_SELECTOR, "td.padding > span").get_attribute("innerText"))
#val = driver.find_element(By.XPATH,"//table[@id='rt_NS_']//tbody//tr/td//div//table[2]//tbody/tr/td[2]/span").get_attribute('innerText')
#val = driver.find_element(By.CLASS_NAME,'lnXdpd').get_attribute('alt')
#val = driver.find_element(By.CLASS_NAME,'pane paneContent contentViewPane')
# tablr = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="mainViewerTable_NS_"]'))).get_attribute("innerText")
# df1 = pd.read_html(tablr)[0]
#print(val)
# day_lists=WebDriverWait(driver, 30).until(EC.visibility_of_all_elements_located((By.XPATH, '//li[@class="pg"]')))
# for day_list in day_lists:
# print(day_list.find_element_by_xpath("//span[*[text()='2023']").text)
# day_lists = driver.find_elements(By.NAME,'td')
# for day_list in day_lists:
# print(day_list.find_element(By.XPATH,"//span[*[text()='2023']").get_attribute('innerText'))
#find_element(By.ID, "CVReport_NS_")
# pathh = "C:/Users/vk05509/Desktop/bla.jpg"
# driver.save_screenshot(pathh)
driver.quit()
2
Answers
You can get the data directly from td tag instead of accessing span tag
Java Code:
Iterate as required
I omitted a part and hope you can understand. "rows" represents the XPath for each row in the table,it is a list containing elements. While iterating through each row, perform operations on each cell in the row. Code in python:
I’m not sure if the xpath I wrote is correct, please make sure the xpath is correct.