在 Python 中使用 Selenium 的数据列表中的字段

问题描述 投票:0回答:1

我有一个 python 脚本,使用 selenium 来自动化网页。我有一个屏幕,其中显示了一个包含 cuits 列表的框,自动化机器人必须在这些 cuits 中搜索与称为“represented”的列相匹配的 cuits,然后按 TAB 键并使用键盘向上或向下移动,直到找到与所代表的相匹配的剪辑。但是,我无法让它在该页面上执行任何操作。这是结构:

import requests 
import json from selenium 
import webdriver from selenium.webdriver.common.by 
import By from selenium.webdriver.common.keys 
import Keys from selenium.webdriver.common.action_chains 
import ActionChains import time from selenium.webdriver.support.ui 
import Select from selenium.webdriver.support.ui 
import WebDriverWait from selenium.webdriver.support 
import expected_conditions as EC 
import pandas as pd 
# Read the Excel file df = pd.read_excel(r"./data_clientes.xlsx") 
# Browser configuration driver = webdriver.Chrome() 
try: 
# Navigate to the page AFIP
driver.get("https://auth.afip.gob.ar/contribuyente_/login.xhtml")

# Iterate over each row of the DataFrame
for index, row in df.iterrows():
name = row['Name']
cuit = row['Cuit']
password = row['Password']
represented = row['Represented']

# Wait for the page to load
time.sleep(3)

# Find the CUIT field and send the CUIT
cuit_input = driver.find_element(By.ID, "F1:username")
cuit_input.clear()
cuit_input.send_keys(cuit)

# Find the "Next" button and click on it
next_button = driver.find_element(By.ID, "F1:btnNext")
next_button.click()

# Wait a moment for the page to process the click
time.sleep(2)

# Find the key field and send the key
input_key = driver.find_element(By.ID, "F1:password")
input_key.clear()
input_key.send_keys(password)

# Submit the form
input_key.send_keys(Keys.RETURN)

# Wait a moment to make sure the page has fully loaded
time.sleep(3)

# Find the search field on the AFIP home page
buscador_input = driver.find_element(By.ID, "buscadorInput")

# Enter "CCMA" in the search field
buscador_input.send_keys("CCMA")

# Submit the search form
buscador_input.send_keys(Keys.RETURN)

# Wait a moment for the search to complete process
time.sleep(3)

# Select the "CCMA" element in the search results
ccma_elemento = driver.find_element(By.XPATH, "/html/body/div/div/div[2]/section/div/div/div[2]/div/div/div[1]/div/div/ul/li[1]/a/div/div/div[1]/div/p")
ccma_elemento.click()
time.sleep(10)
# If I am on a CCMA page with several representatives
# Check if we are on the CUIT selection page
if driver.current_url == "https://servicios2.afip.gob.ar/tramites_con_clave_fiscal/ccam/seleccionaCuit.asp":
cuit_representado=driver.find_element(By.ID, "representado")
cuit_representado.send_keys(Keys.TAB)
cuit_representado.send_keys(Keys.ARROW_DOWN)

# enter the represented cuit
enter_cuit = driver.find_element(By.XPATH, "/html/body/table/tbody/tr[2]/td[2]/table/tbody/tr/td/div[2]/form/div[2]/input")
enter_cuit.click()
else:
continue
# Go to the login page for the next client
driver.get("https://auth.afip.gob.ar/contribuyente_/login.xhtml")

finally:
# Close the browser
driver.quit()

我希望脚本能够读取并用键盘移动到搜索到的cuit,然后自动化过程将继续。

python selenium-webdriver web-scraping
1个回答
0
投票

我有两点。

1. 关于你的代码,

enter_cuit = driver.find_element(By.XPATH, "/html/body/table/tbody/tr[2]/td[2]/table/tbody/tr/td/div[2]/form/div[2]/input")

我刚刚检查了“seleccionaCuit.asp”,发现您的元素索引不正确。因为它们是“列表”并且索引以“0”开头,所以索引“2”应该替换为“1”。
由于您不能在 XPATH 中包含索引,因此您应该将代码分为 4 个步骤。 但在本例中,由于名称“form1”是唯一的,因此您可以按如下两步编写。

divs1 = driver.find_elements(By.XPATH, "//form[@name='form1']/div")
enter_cuit = divs1[1].find_element(By.XPATH, "./input")

请注意,在第一行中我们必须使用“find_elements”而不是“find_element”。

或者,如果此页面中只有一个“输入”元素,则可以简单地写为

enter_cuit = driver.find_element(By.XPATH, "//input")

不要对多行Python代码使用“try”。因为在“try”覆盖的区域内,任何错误都会被传递,而不会显示任何消息,因此您无法找到错误出现的位置和原因。将使用限制在无法找到任何其他方法来转义异常(错误)的特殊情况,并限制单行 python 代码。

© www.soinside.com 2019 - 2024. All rights reserved.