如何用python填写web表单

2024-10-02 14:27:08 发布

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

我试图通过python脚本填写此表单: https://reassurez-moi.fr/devis-assurance-pret-immobilier/60773413ea18bd60703427df9c39cbd2c5c1c8d46fda062f92513e1c266bbfb5

但不幸的是,我找不到允许我从下拉列表中选择值的代码

您可以注意到,在html中,我们没有下拉列表中的不同选项

你能告诉我如何制作一个python脚本,让我填写这个表单吗

先谢谢你

下面的代码显示了我迄今为止所做的工作,但它不允许我从下拉列表中选择一个值

from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
web1 = webdriver.Chrome(ChromeDriverManager().install())
web1.get('https://reassurez-moi.fr/simulation-tarifaire-assurance-habitation?homeType=1')
#time.sleep(2)
topics_xpath = '//*[@id="home-comparator-base"]/div[2]/div/div[2]/div/form/div[1]/div[1]/div/div[1]/div[2]/div/div[2]/button'
WebDriverWait(web1,2).until(expected_conditions.visibility_of_element_located((By.XPATH, topics_xpath)))
Rue_Adresse = "Rue de la Paix"
last = web1.find_element_by_xpath('//*[@id="home-comparator-base"]/div[2]/div/div[2]/div/form/div[3]/div[1]/div[2]/div/input')
last.send_keys(Rue_Adresse)
ast = web1.find_element_by_xpath('//*[@id="home-comparator-base"]/div[2]/div/div[2]/div/form/div[1]/div[1]/div/div[1]/div[2]/div/div[2]/button')
ast.click()

Tags: fromimportdivformidhome列表base
1条回答
网友
1楼 · 发布于 2024-10-02 14:27:08

您最好在这个特定的网站上使用Xpath来突出显示所有不同的输入值。右键单击要输入信息的元素,单击inspect,然后右键单击该元素并单击“Copy Xpath”。从那里我将使用Pyautogui,这将允许您接管键盘。然后,您可以使用pyautogui.hotkey(“向下”)向下滚动到所需的选择。您还可以使用pyautogui.write('text here')填充不同的文本框。Pyautogui可以很好地与selenium配合使用,您可以使用selenium激活要使用的区域,并使用Pyautogui对其进行操作

相关问题 更多 >