rvictordelta rvictordelta - 1 month ago 12
Python Question

Scraping with Python Selenium: iterate through html table without tags

I'm a Python neophyte working on a fun little scraping project. Trying to pull info from here:
http://www.airfleets.net/flottecie/American%20Airlines.htm

I'm in Python 2 and using Selenium

There's a table on the page with aircraft details. I want to iterate through the second column of this table, which is labeled "Active". Normally, I would select the table by using

find_element_by_id
. However, this table doesn't have an
id
tag. I think I need to find the table by
find_element_by_xpath
, but I am unsure of the path syntax to find the table and then also the rows in the second column.

In summary, how can I iterate through the rows of a table if the table does not have any identifying tags?

Answer

You could use CSS selector or XPath. As mentioned in the comments, your browser's dev tools probably have a builtin way to do this.

That table's Xpath is

/html/body/table[4]/tbody/tr[1]/td/table[2]/tbody/tr/td[2]/table 

And a CSS selector that you can use is

body > table:nth-child(6) > tbody > tr:nth-child(1) > td > table:nth-child(3) > tbody > tr > td:nth-child(2) > table

In Chrome, for example, you can obtain this information in the following way:

(1) Open up the dev tools and find the element. You can do this by right-clicking any element and clicking "inspect"

enter image description here

2) Right-click the element in the DOM, then select Copy > (Copy Selector / Copy XPathenter image description here

Comments