在web表单结果中查找包含Selenium的元素时出错

2024-07-08 09:40:22 发布

您现在位置:Python中文网/ 问答频道 /正文

有人能帮我写这段代码吗

我正在努力实现这一目标,但我在完成这一部分时遇到了一点困难:

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()

进入此网页:http://www1.cfc.org.br/sisweb/Registro/ConsultaCNPC 在第二个框中显示此数据:336.174.128-90

我得到了这个错误:

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

我对python还是新手


Tags: textingetbyifattributeelementfind
2条回答

看起来./td[2]<tr class='odd'>下只有一个td元素的情况下抛出了NoSuchElementException。如果表中没有结果,则会发生这种情况

这可以通过在调用driver.find_element_by_xpath("./td[2]")之前检查子元素的计数来解决。我将重构这段代码,以便更清楚地了解有搜索结果与没有搜索结果的场景

另外,我注意到一些搜索结果显示<tr class='odd'>和其他<tr class='even'>。我不确定您是否打算排除even类行,因此我将为这两个行提供一个示例

下面的示例使用“CPF”字段进行搜索,然后等待结果行出现。代码在结果行中循环并打印出单元格文本。如果表中没有结果,循环将中断

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)

我运行的测试的输出:

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

要获取第一行的所有列,可以使用//table[@id='table-cnpc']//tr[1]//td[not(@class='dataTables_empty')]xpath。此xpath还处理空结果

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")

相关问题 更多 >

    热门问题