Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 99407

Problem with IF validating XPATH in Python with Selenium

$
0
0

Im trying to scrape one ASPX form, and i`m having difficulties on make one IF for select different types os results, i have blank results first and data results second.

How do i make one IF to validate one XPATH? IF the XPATH is true i wanna do one thing, ELSE i wanna do another thing.

That`s my code:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import openpyxl
from openpyxl import load_workbook
driver = webdriver.Chrome(executable_path=r'C:\Python37\webdriver\chromedriver.exe')

driver.get("https://www3.cfc.org.br/SPw/ConsultaNacional/ConsultaCadastralCFC.aspx")

planilha = openpyxl.load_workbook('BASE 05-09.xlsx')

sheet = planilha['Aba1']

for Count in range(2,5552):

    box_cpf = driver.find_element_by_xpath(".//*[@id='ctl00_ContentPlaceHolder1_TxtCpf']")
    box_cpf.send_keys(sheet.cell(row=Count, column=5).value)

    pesquisar_bto = driver.find_elements_by_id("ctl00_ContentPlaceHolder1_btPesquisar")[0]
    pesquisar_bto.click()

    if driver.find_element_by_xpath("/html/body/form/div[3]/div[2]/div/table[2]/tbody/tr[4]/td/table[3]/tbody/tr/td/table[1]/tbody/tr[2]/td/div"):
        resultado_pesquisa = driver.find_element_by_xpath("/html/body/form/div[3]/div[2]/div/table[2]/tbody/tr[4]/td/table[3]/tbody/tr/td/table[1]/tbody/tr[2]/td/div")
        sheet.cell(row=Count, column=6).value = resultado_pesquisa.text
    else:
        resultado_pesquisa = driver.find_element_by_xpath("/html/body/form/div[3]/div[2]/div/table[2]/tbody/tr[4]/td/table[3]/tbody/tr/td/table[1]/tbody/tr[2]/td[2]")
        sheet.cell(row=Count, column=6).value = resultado_pesquisa.text

    planilha.save("BASE 05-09.xlsx")
    time.sleep(.5)

    limpar_bto = driver.find_elements_by_id("ctl00_ContentPlaceHolder1_btLimpar")[0]
    limpar_bto.click()
    time.sleep(.5)

driver.quit()

That`s the XPATH when i have empty result:

"/html/body/form/div[3]/div[2]/div/table[2]/tbody/tr[4]/td/table[3]/tbody/tr/td/table[1]/tbody/tr[2]/td/div"

And the XPATH when i have results:

"/html/body/form/div[3]/div[2]/div/table[2]/tbody/tr[4]/td/table[3]/tbody/tr/td/table[1]/tbody/tr[2]/td[2]"

Sorry if im asking something to obvious, im new in python


Viewing all articles
Browse latest Browse all 99407

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>