我正在网站中进行自动搜索,但是当我单击按钮或提交表单时,网站会重新加载并删除我的过滤器。我认为问题出在网站的一项功能上。
这是网站:
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"
我的硒和蟒蛇代码:
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()
尝试使用此xpath
单击"搜索"按钮:
.//div[@class='botoes-consulta']/a/font/font[text()='Search ']
这也不是id
xpath
driver.find_element_by_id('//*[@id="formPesquisaJurisprudencia"]/div[5]/div/a[1]').click()
单击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()