Selenium chromedriver不执行onclick事件

时间:2019-06-21 18:17:48

标签: python selenium jsf

我正在开发一种机器人,用于从特定网站上抓取一些信息。 在此网站上,我们需要填写以下动态表格:

  • 健康计划(选择);
  • 状态地址(选择)
  • 城市地址(动态选择,其中包含前一个字段加载的数据)
  • 搜索方式(单选按钮)

在Chrome中,此单选按钮会将我们带到另一个界面(具有相同的网址),但在Chromedriver中则不会。

我已经检查了此单选按钮中的代码,并具有以下onclick事件:

jsf.ajax.request('viewns_Z7_MPD81G80P843E0QQK0CK5L2GC1_:formInicio:tipoPesquisa',event,{execute:'@this viewns_Z7_MPD81G80P843E0QQK0CK5L2GC1_:formInicio:panelgrid-g-recaptcha-response ',render:'viewns_Z7_MPD81G80P843E0QQK0CK5L2GC1_:formBuscaSobreOMapa viewns_Z7_MPD81G80P843E0QQK0CK5L2GC1_:formInicio viewns_Z7_MPD81G80P843E0QQK0CK5L2GC1_:formInicio:panelGridSelectOneRadioTipoPesquisa ',onerror:mostrarErroGenerico,onevent:selecionarTipoPesquisa,'javax.faces.behavior.event':'click'})

我该如何通过?

(我尝试在网址栏中运行此javascript代码,并手动执行所有步骤,但不起作用。)

我的整个代码:

#!/usr/bin/env python
# coding: utf-8

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# Create a browser instance
gc = webdriver.Chrome()
gc.maximize_window()

# Open the url
gc.get('https://www.bradescoseguros.com.br/clientes/produtos/plano-saude/consulta-de-rede-referenciada')

time.sleep(2)

# Select "Non Client" mode
non_client = gc.find_element_by_name('viewns_Z7_MPD81G80P843E0QQK0CK5L2GC1_:formInicio:clNotClient')
non_client.click()

time.sleep(2)

# Get plans
lp = gc.find_element_by_id('lista-redes')
lp_list = lp.find_element_by_tag_name('ul')
lp_options = lp_list.find_elements_by_tag_name('li')
lp_options[0].click()

time.sleep(2)

# Select states
le = gc.find_element_by_id('lista-estado')
le_list = le.find_element_by_tag_name('ul')
le_options = le_list.find_elements_by_tag_name('li')

states_dict = {
    'ACRE': '00',
    'ALAGOAS': '01',
    'AMAZONAS': '02',
    'AMAPA': '03',
    'BAHIA': '04',
    'CEARA': '05',
    'DISTRITO FEDERAL': '06',
    'ESPIRITO SANTO': '07',
    'GOIAS': '08',
    'MARANHAO': '09',
    'MINAS GERAIS': '10',
    'MATO GROSSO DO SUL': '11',
    'MATO GROSSO': '12',
    'PARA': '13',
    'PARAIBA': '14',
    'PERNANBUCO': '15',
    'PIAUI': '16',
    'PARANA': '17',
    'RIO DE JANEIRO': '18',
    'RIO GRANDE DO NORTE': '19',
    'RONDONIA': '20',
    'RORAIMA': '21',
    'RIO GRANDE DO SUL': '22',
    'SANTA CATARINA': '23',
    'SERGIPE': '24',
    'SAO PAULO': '25',
    'TOCANTINS': '26'
}

# Select SP state
le_options[25].click()

time.sleep(2)

# Select cities
lc = gc.find_element_by_id('lista-cidade')
lc_list = lc.find_element_by_tag_name('ul')
lc_options = lc_list.find_elements_by_tag_name('li')

i = 0
for c in lc_options:
    print('ID: {}\nCidade:{}\n'.format(i, c.find_element_by_tag_name('a').text))
    i+=1

lc_options[110].click()

time.sleep(2)

# Select a establishment
les = gc.find_element_by_id('lista-tipo-pesquisa')
les_options = les.find_elements_by_xpath('//input[@type="radio"]')

i = 0
for r in les_options:
    print('ID: {}\nOpcoes:{}\n'.format(i, r.get_attribute('value')))
    i+=1

time.sleep(2)

les_options[2].click()

#gc.find_element_by_class_name('form').find_element_by_tag_name('form').submit()

# Close Browser
#gc.quit()

1 个答案:

答案 0 :(得分:0)

这是经过完美测试和运行的脚本。

driver.find_element_by_class_name("clNotClient").click()
# wait until the loading is done
WebDriverWait(driver,10).until_not(EC.presence_of_element_located((By.CSS_SELECTOR,".panelGridRecaptchaResponse[style*='display: none'] img")))
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR,".panelGridRecaptchaResponse[style*='display: none'] img")))
listItems = driver.find_elements_by_css_selector("#lista-redes  .list-group-item.ui-menu-item")
listItems[0].click()


# wait until the loading is done
WebDriverWait(driver,10).until_not(EC.presence_of_element_located((By.CSS_SELECTOR,".panelGridRecaptchaResponse[style*='display: none'] img")))
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR,".panelGridRecaptchaResponse[style*='display: none'] img")))
npList = driver.find_elements_by_css_selector(".tbl-dados .list-group-item.ui-menu-item")
npList[25].click()

# wait until the loading is done
WebDriverWait(driver,10).until_not(EC.presence_of_element_located((By.CSS_SELECTOR,".panelGridRecaptchaResponse[style*='display: none'] img")))
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR,".panelGridRecaptchaResponse[style*='display: none'] img")))
#cities
cities = driver.find_elements_by_css_selector("#lista-cidade li.list-group-item.ui-menu-item")
cities[-1].click()

# wait until the loading is done
WebDriverWait(driver,10).until_not(EC.presence_of_element_located((By.CSS_SELECTOR,".panelGridRecaptchaResponse[style*='display: none'] img")))
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR,".panelGridRecaptchaResponse[style*='display: none'] img")))
# select radio
radios = driver.find_elements_by_css_selector("#lista-tipo-pesquisa input[type='radio']")
radioNumber = 1
for radio in radios:
    print ("Radion " + str(radioNumber) + ":- " + radio.find_element_by_xpath("./following-sibling::label").text)
    radioNumber = radioNumber+1
radios[2].click()

enter image description here