Titio Buga
Titio Buga

Reputation: 21

Error finding element with Selenium into a web form result

Can someone help me in this code?

Im trying to make this scraper happens, but i'm having a little trouble finishing this part:

driver.find_element_by_id("Cpf").send_keys(sheet.cell(row=Count, column=2).value, Keys.RETURN)

results = wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "table[class*='table table-striped table-bordered dataTable'] > tbody > tr[class*=odd]")))
resultado_cnpc = results[0].text.strip() if "EmptyDataRow" in results[0].get_attribute("class") else results[0].find_element_by_xpath("./td[1]").text.strip()
resultado_nome = results[0].text.strip() if "EmptyDataRow" in results[0].get_attribute("class") else results[0].find_element_by_xpath("./td[2]").text.strip()
resultado_registro = results[0].text.strip() if "EmptyDataRow" in results[0].get_attribute("class") else results[0].find_element_by_xpath("./td[3]").text.strip()
resultado_uf = results[0].text.strip() if "EmptyDataRow" in results[0].get_attribute("class") else results[0].find_element_by_xpath("./td[4]").text.strip()
resultado_inclusao = results[0].text.strip() if "EmptyDataRow" in results[0].get_attribute("class") else results[0].find_element_by_xpath("./td[5]").text.strip()

Into this web page: http://www1.cfc.org.br/sisweb/Registro/ConsultaCNPC with this data on second box: 336.174.128-90

I'm getting this error:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"./td[2]"}

I`m still new into python

Upvotes: 1

Views: 100

Answers (2)

Sers
Sers

Reputation: 12265

To get all columns for the first row you can use //table[@id='table-cnpc']//tr[1]//td[not(@class='dataTables_empty')] xpath. This xpath handels empty results also.

row = wait.until(EC.visibility_of_element_located(
    (By.XPATH, "//table[@id='table-cnpc']/tbody/tr[1]")))

results = row.find_elements_by_xpath(".//td[not(@class='dataTables_empty')]")
# or css selector
# results = row.find_elements_by_css_selector("td:not(.dataTables_empty)")
if results:
    resultado_cnpc = results[0].text.strip()
    resultado_nome = results[1].text.strip()
    resultado_registro = results[2].text.strip()
    resultado_uf = results[3].text.strip()
    resultado_inclusao = results[4].text.strip()
else:
    print("Não existe registro")

Upvotes: 0

CEH
CEH

Reputation: 5909

It looks like ./td[2] is throwing the NoSuchElementException in the case where is only one td element under <tr class='odd'>. This happens in cases where there are no results in the table.

This can be resolved by checking the count of the child elements before calling driver.find_element_by_xpath("./td[2]"). I would refactor this code to be a bit more clear on the scenario where there are search results vs. no search results.

Also, I noticed some search results show <tr class='odd'> and others <tr class='even'>. I am not sure if you intended to exclude the even class rows, so I will include a sample for both.

The following sample searches using the 'CPF' field, then waits for result rows to appear. The code loops through the result rows and prints out cell text. If there are no results present in the table, the loop will break.

driver = webdriver.Chrome()
driver.get("http://www1.cfc.org.br/sisweb/Registro/ConsultaCNPC")

wait = WebDriverWait(driver, 30)

# search in CPF
wait.until(EC.presence_of_element_located((By.ID, "Cpf"))).send_keys("336.174.128-90" + Keys.ENTER)

# use this XPath to wait on all rows in the table -- rows are either class='odd' or class='even'
results = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//tr[@class='odd'] | //tr[@class='even']")))

# optional: use below line to exclude 'even' rows:
#results = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//tr[@class='odd']")))

# loop through result rows
for result in results:

    # get child td elements under this row
    child_elements = result.find_elements_by_xpath("td")

    # if there is only one td child element, then no results are present in the table
    if len(child_elements) == 1:
        print("No results returned")
    else:

        # if row has child elements, loop through elements and print text
        for child in child_elements:
            print(child.text)

Output from the test I ran:

420
FRANCISCO ANTONIO PARADA VAZ FILHO
SP-253063 / O
CRC-SP
17/06/2016
Detalhes

Upvotes: 2

Related Questions