python - 使用 Selenium 在 Web 表单结果中查找元素时出错

标签 python selenium

有人可以帮我编写这段代码吗?

我试图让这个抓取发生,但我在完成这部分时遇到了一些麻烦:

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新手

最佳答案

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

这可以通过在调用 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

关于python - 使用 Selenium 在 Web 表单结果中查找元素时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59902255/

相关文章:

python - Django: 'str' 对象没有属性 'user'

testing - 如何在 JUnit 4 Webdriver 中遍历父 dom 元素并获取 id?

c# - 等待 Selenium Webdriver 更改的最佳实践?

python - 在 Pandas 数据框中定位元素时减少执行时间

python - (扩展)用户表格

Python导入类型检测

javascript - 错误 : done() invoked with non-Error: {}

python - 在 NumPy 中按行比较两个矩阵

java - 有没有办法使用 findElements 并将结果限制为前 x 项

python - 如何调用名称与数字分隔的函数?从 t1 到 t20 调用函数的示例 - Python?