skip to Main Content

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


  1. You can get the data directly from td tag instead of accessing span tag

    Java Code:

    WebElement TableName=driver.findElement(By.Xpath("Xpath of Table"));
    
    java.util.List<Webelement> tablerowlist=TableName.findElements(By.tagname("tr")); //Storing all the rows in the webtable in a list
    
    WebElement specificRowdata=tablerowlist.get(1); //1 is the row number
    
    java.util.List<Webelement> tableColumnlist=specificRowdata.findElements(By.tagname("td")); //Storing all the column data in row number 1 in a list
    
    WebElement specificColumndata=tableColumnlist.get(2); //2 is the column number
    System.out.println(specificColumndata.getText())
    

    Iterate as required

    Login or Signup to reply.
  2. 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:

    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as ec
    from selenium.webdriver.common.by import By
    ...
    rows = WebDriverWait(driver, timeout=5).until(ec.presence_of_all_elements_located((By.XPATH, '//table[@id="rt_NS_"]//tbody//tr/td//div//table[2]//tbody/tr')))
    for row in rows:
        datetime = row.find_element(By.XPATH, './td[2]/span').text
        
    

    I’m not sure if the xpath I wrote is correct, please make sure the xpath is correct.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search