我刚接触过Selenium,所以我很抱歉,如果这是个菜鸟的问题。
我正试图用Python和Selenium实现文档的自动生成。
当我使用自动firefox或chrome驱动程序时,在最后一步之后,即单击按钮“gerar2a or”(xpath = '//* @id=“btnGerarSegundaVia”),页面就会冻结,除了一个甚至无法正确加载的图标之外什么也不会显示。开发人员的工具中的网络选项卡中也有一些jquery错误。
另一件有趣的事情是,当您试图通过自动浏览器重新加载页面时,服务器将返回PR_CONNECT_RESET_ERROR。
在你的头上模仿这个问题:
普通浏览器:
自动浏览器:
我只编写了自动浏览器的打开代码来测试一些选项,没有一个注释掉的代码解决了我的问题。
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.firefox.options import Options
# profile_path = r'whatever'
# options = Options()
# PROXY_HOST = "12.12.12.123"
# PROXY_PORT = "1234"
# options.set_preference('profile', profile_path)
# options.set_preference("network.proxy.type", 1)
# options.set_preference("network.proxy.http", PROXY_HOST)
# options.set_preference("network.proxy.http_port", int(PROXY_PORT))
# options.set_preference("dom.webdriver.enabled", False)
# options.set_preference('useAutomationExtension', False)
# options.add_argument('--ignore-certificate-errors')
# desired_caps = DesiredCapabilities.FIREFOX.copy()
# desired_caps.update({'acceptInsecureCerts': True, 'acceptSslCerts': True})
driver = webdriver.Firefox()
driver.get('https://iptu.prefeitura.sp.gov.br/')
如果我不得不猜测的话,问题是站点检测到‘连接不安全’,因为Selenium控制浏览器,然后阻止服务器的响应,但是你们比我聪明,所以请HEEEELP。
谢谢
发布于 2022-08-12 20:02:30
以下是插入captcha文本的点,然后单击按钮,它将带您到pdf页面:
import undetected_chromedriver as uc
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = uc.Chrome()
url = 'https://iptu.prefeitura.sp.gov.br/'
browser.get(url)
contrib_field = WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='txtNumeroContribuinte']")))
contrib_field.send_keys('019.045.0170-2')
parcel_field = Select(WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='comboBoxParcela']"))))
parcel_field.select_by_index(7)
ex_field = WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='txtExercicio']")))
ex_field.send_keys('2022')
关于undetected_chromedriver包:https://pypi.org/project/undetected-chromedriver/
https://stackoverflow.com/questions/73338506
复制相似问题