【问题标题】:How to fill in a web form with python如何使用python填写网络表单
【发布时间】:2021-04-15 07:15:18
【问题描述】:

我正在尝试通过 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()

【问题讨论】:

  • 为什么不使用更好的 xpath 并使用 clicsk 来“打开”下拉菜单并选择您请求的值

标签: python html selenium selenium-webdriver python-requests


【解决方案1】:

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

【讨论】:

  • 感谢您的回答,但我无法选择我需要的内容。事实上,pyautogui 在用 Selenium 打开的激活区域中不起作用。它适用于我的 jupyter 页面,而不是我需要它工作的地方。你有什么建议吗?谢谢。
猜你喜欢
  • 2013-04-14
  • 1970-01-01
  • 1970-01-01
  • 2016-09-11
  • 1970-01-01
  • 2019-02-26
  • 2020-12-06
  • 1970-01-01
相关资源
最近更新 更多