【发布时间】:2019-11-14 18:22:07
【问题描述】:
在 Python 3 和 selenium 中,我有这个脚本可以自动搜索包含公共信息的站点中的术语
from selenium import webdriver
# Driver Path
CHROME = '/usr/bin/google-chrome'
CHROMEDRIVER = '/home/abraji/Documentos/Code/chromedriver_linux64/chromedriver'
# Chosen browser options
chrome_options = webdriver.chrome.options.Options()
chrome_options.add_argument('--window-size=1920,1080')
chrome_options.binary_location = CHROME
# Website accessed
link = 'https://pjd.tjgo.jus.br/BuscaProcessoPublica?PaginaAtual=2&Passo=7'
# Search term
nome = "MARCONI FERREIRA PERILLO JUNIOR"
# Waiting time
wait = 60
# Open browser
browser = webdriver.Chrome(CHROMEDRIVER, options = chrome_options)
# Implicit wait
browser.implicitly_wait(wait)
# Access the link
browser.get(link)
# Search by term
browser.find_element_by_xpath("//*[@id='NomeParte']").send_keys(nome)
browser.find_element_by_xpath("//*[@id='btnBuscarProcPublico']").click()
# Searches for the text of the last icon - the last page button
element = browser.find_element_by_xpath("//*[@id='divTabela']/div[2]/div[2]/div[4]/div[2]/ul/li[9]/a").text
element
'»'
此站点在搜索术语时对结果进行分页,并始终将“»”按钮显示为最后一个分页按钮。
案例中倒数第二个按钮将是“›”
所以我需要在最后一个之前捕获按钮文本两次。这是这种情况下的数字“8”,用于自动更改页面 - 我会知道需要多少次点击下一页
请问,当我搜索 Xpath 时,我如何捕获之前两个位置的元素?
【问题讨论】:
标签: python selenium xpath tags selenium-chromedriver