Selenium/Python - Extract dynamically generated HTML after submitting form(Selenium/Python - 提交表单后提取动态生成的 HTML)
问题描述
我尝试访问的网页是使用 JavaScript 动态生成 HTML 表单(这个:https://imgur.com/a/rhmXB).输入 print(page_source)
时,表格似乎出现在输出的 HTML 中.
The web page I am trying to access is using JavaScript to dynamically generate HTML form(this one: https://imgur.com/a/rhmXB ). When typing print(page_source)
, the table seems to appear in the HTML being outputted.
然而,在填写输入字段并提交表单后,会出现另一个带有验证码图像的输入字段(如下所示:https://imgur.com/a/xVfBS ).输入 print(page_source)
后,带有 CAPTCHA 的输入表单似乎没有插入到 HTML 中.
However, after filling the input field and submitting the form, another input field with CAPTCHA image appears(as shown here: https://imgur.com/a/xVfBS ). After typing print(page_source)
, the input form with the CAPTCHA seems not to be inserted into the HTML.
我的问题是:如何使用 Selenium 访问这个动态生成的 HTML,其中包含输入字段和验证码图像?
My question is: How can I access this dynamically generated HTML, which contains the input field and the CAPTCHA image using Selenium?
这是我的代码(另外,in pastebin):
Here is my code (also, in pastebin):
from selenium import webdriver
driver = webdriver.Chrome("/var/chromedriver/chromedriver")
URL = 'http://nap.bg/link?id=104'
driver.get(URL)
input_field = driver.find_element_by_name('ipID')
input_field.send_keys('0000000000')
driver.find_element_by_id('idSubmit').click()
print(driver.page_source)
推荐答案
点击按钮后,页面需要一些时间来加载验证码等内容.您需要等待它完成加载.您可以使用 Selenium 的 显式等待来做到这一点.
After you click on the button, the page takes some time to load the CAPTCHA and other content. You'll need to wait for that to finish loading. You can do that using Selenium's explicit waits.
这是你可以做的一个例子:
This is an example for what you can do:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
URL = 'http://nap.bg/link?id=104'
driver.get(URL)
input_field = driver.find_element_by_name('ipID')
input_field.send_keys('0000000000')
driver.find_element_by_id('idSubmit').click()
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.NAME, 'ipResponse')))
print(driver.page_source)
这篇关于Selenium/Python - 提交表单后提取动态生成的 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Selenium/Python - 提交表单后提取动态生成的 HTML


基础教程推荐
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 包装空间模型 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01