gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:52:22 29 4
gpt4 key购买 nike

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

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

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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com