Home > Enterprise >  Find elements by Class returning blank list
Find elements by Class returning blank list

Time:08-15

I have a code to pull the vessels from this website:

'https://signal.portoptimizer.com/'

I was able to open the table with the vessels using

arrow_icon = self.driver.find_element('xpath','/html/body/app-root/app-pure-frame/div/div/div/app-signal2-page/div/div/app-current-inbound-vessels/div/app-widget-card/div/div[2]/div/mat-expansion-panel/mat-expansion-panel-header/span[2]')
arrow_icon.click()

but now I want to have a list with all vessesls, tried with the below but it is showing no value in the list:

    all_vessels = self.driver.find_elements(By.CLASS_NAME,"anchorage-by-terminal")
    print(all_vessels)

What would be the issue?

Thanks in advance!

CodePudding user response:

You can get all the the vessel names as following:
First get all the table rows and then get each row first cell text, like this:

rows = self.driver.find_elements(By.CSS_SELECTOR,"datatable-row-wrapper datatable-body-row")
for row in row:
    name = row.find_element(By.CSS_SELECTOR,"span.anchorage-by-terminal-table-cell-content").text
    print(name)

"span.anchorage-by-terminal-table-cell-content" is not a unique locator for the first cell in the row, but since we are looking for a first call in each row this will work here.
Also, don't forget using delays, preferably expected conditions explicit waits to make page / elements loaded before you access them.

CodePudding user response:

You want to find a class given only part of the name (in this case the beginning). You can do this with CSS selectors:

self.driver.find_elements(By.CSS_SELECTOR, "div[class^='anchorage-by-terminal']")
  • Related