I am trying to scrape some information from a URL as the figure. Sometimes one item is available in one line and other in two different lines. There is no standard when it is in one line or two.
When is only in one line, I use [i+1]
. While in two lines, I use [2*i+1]
. What is the best way to continue the loop until no more lines are available in these two different conditions?
for i, registro in enumerate(elements):
if browser.find_element_by_xpath(
f'//*[@id="grdDocumentos"]/tbody/tr[{i+1}]/td').text:
try:
empresa = browser.find_element_by_xpath(
f'//*[@id="grdDocumentos"]/tbody/tr[{i+1}]/td[2]').text
except:
empresa = browser.find_element_by_xpath(
f'//*[@id="grdDocumentos"]/tbody/tr[{2*i+1}]/td[2]').text
The code line if browser.find_element_by_xpath(f'//*[@id="grdDocumentos"]/tbody/tr[{i+1}]/td').text:
is responsible to find items with two lines.
Aucun commentaire:
Enregistrer un commentaire