当我单击带有硒的搜索按钮时,网站重新加载

时间:2020-01-27 19:11:24

标签: python selenium selenium-webdriver selenium-chromedriver

我正在网站上进行自动搜索,但是当我单击按钮或提交表单时,网站会重新加载并删除我的过滤器。我认为问题出在网站的一个功能上。

这是网站:

http://www.tjpe.jus.br/consultajurisprudenciaweb/xhtml/consulta/consulta.xhtml

功能

function dpf(f) {var adp = f.adp;if (adp != null) {for (var i = 0;i < adp.length;i++) {f.removeChild(adp[i]);}}};function apf(f, pvp) {var adp = new Array();f.adp = adp;var i = 0;for (k in pvp) {var p = document.createElement("input");p.type = "hidden";p.name = k;p.value = pvp[k];f.appendChild(p);adp[i++] = p;}};function jsfcljs(f, pvp, t) {apf(f, pvp);var ft = f.target;if (t) {f.target = t;}f.submit();f.target = ft;dpf(f);};

在网站上有这样一个“点击”:

onclick="if(typeof jsfcljs == 'function'){jsfcljs(document.getElementById('formPesquisaJurisprudencia'),{'formPesquisaJurisprudencia:j_id112':'formPesquisaJurisprudencia:j_id112'},'');}return false"

我的硒和python代码:

import time
import os
import seleniumrequests
from selenium import webdriver
from seleniumrequests import Chrome
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

chrome_options = Options()
chrome_options.add_experimental_option('prefs',  {
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "plugins.always_open_pdf_externally": True
    }
)

estado = str(input("Em qual estado você deseja fazer a busca?\n")) 

if (estado.upper() == 'PE'): #Mudar para "Case" quando houver outros estados
    med = str(input("Qual medicamento você deseja procurar?\n"))

    driver = webdriver.Chrome()

    driver.get('http://www.tjpe.jus.br/consultajurisprudenciaweb/xhtml/consulta/consulta.xhtml')

    driver.find_element_by_id('formPesquisaJurisprudencia:inputBuscaSimples').send_keys(med)

    driver.find_element_by_id('//*[@id="formPesquisaJurisprudencia"]/div[5]/div/a[1]').click()

    driver.find_element_by_xpath('//*[@id="resultadoForm:j_id20_body"]/div/table/tbody/tr[1]/td[2]/a').click()

    driver.find_element_by_name('j_id77:j_id80').click()

2 个答案:

答案 0 :(得分:0)

尝试使用此xpath单击“搜索”按钮:

.//div[@class='botoes-consulta']/a/font/font[text()='Search ']

这也不是id的{​​{1}}

xpath

答案 1 :(得分:0)

要点击Pesquisar链接,请诱导WebDriverWait()element_to_be_clickable并遵循Xpath选项。

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[@class='form-button first' and text()='Pesquisar']"))).click()

或跟随css选择器。

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.botoes-consulta >a.form-button.first"))).click()