In the try block where I have written "#print text details of table 1:" is where I am stuck.
I am looking to extract the text from the 1st table in the popup upon clicking "details". XPATH: /html/body/table[1].
driver.get("https://www.drayage.com/directory/results.cfm?city=SAV&port=y&OceanCntrs=y&drvrs=y&showClicks=y")
trs = wait.until(EC.visibility_of_all_elements_located((By.XPATH,"//html/body/table/tbody/tr/td/table[1]//tr[position()>2]")))
window_before = driver.window_handles[0]
for tr in trs:
try:
detail = tr.find_element(By.XPATH,".//a[contains(.,'detail')]")
detail.click()
wait = WebDriverWait(driver,10)
#Handle the tab switch
window_after = driver.window_handles[1]
driver.switch_to.window(window_after)
sleep(2)
#Print text details of table 1:
#Continue iterating through "detail" popups
driver.close()
driver.switch_to.window(window_before)
except:
print("No detail")
I attempted to iterate through the table body and append the text to a list and print the list but my lists are coming back empty.
#Print text details of table 1:
my_list = []
new_text=driver.find_elements_by_xpath(("//table[@id='dataLstSubCat']/tbody/tr/td"))
for text in new_text:
my_list.append(text.text)
print(my_list)
Here are the imports you will need for my code:
#import packages
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC