在 selenium 中,当我搜索 Xpath 时,如何捕获之前两个位置的元素? [英] In selenium, when I search Xpath how do I capture the element two positions before?

查看:42
本文介绍了在 selenium 中,当我搜索 Xpath 时,如何捕获之前两个位置的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 3 和 selenium 中,我有这个脚本来自动搜索具有公共信息的站点中的术语

In Python 3 and selenium I have this script to automate the search of terms in a site with public information

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
'»'

本网站在搜索术语时会对结果进行分页,并始终将»"按钮显示为最后一个分页按钮.

This site when searching for terms paginates results and always shows as the last pagination button the "»" button.

案例中倒数第二个按钮是›"

The next to last button in the case will be "›"

所以我需要在最后一个之前总是两次捕获按钮文本.这是数字8",用于自动更改页面 - 我将知道下一页需要点击多少次

So I need to capture the button text always twice before the last one. Here is this case the number "8", to automate page change - I will know how many clicks on next page will be needed

请问,当我搜索 Xpath 时,如何捕获之前两个位置的元素?

Please, when I search Xpath how do I capture the element two positions before?

推荐答案

我知道这不是原始问题的答案.但是多次单击下一步按钮并不是一个好习惯.我检查了网络流量,发现他们正在使用偏移参数调用他们的 API url.您应该能够根据需要以适当的偏移量使用此 URL.

I know this is not an answer to the original question. But clicking the next button several times is not a good practice. I checked the network traffic and see that they are calling their API url with offset parameter. You should be able to use this URL with proper offset as you need.

如果确实需要访问最后两个,则需要先获取所有导航按钮,然后按如下方式通过索引访问.

If you really need to access the last but two, you need to get the all navigation buttons first and then access by indexing as follows.

    elems = self.browser.find_elements_by_xpath(xpath)
    elems[-2]

编辑

我刚刚测试了他们的 API,它可以在给定的 cookie 值下正常工作.这种方式比使用 Selenium 的自动化要快得多.使用 Selenium 只是为了提取要在 Web 请求标头中使用的 cookie 值.

EDIT

I just tested their API and it works with proper cookie value given. This way is much faster than automation using Selenium. Use Selenium just to extract cookie value to be used in the header of the web request.

这篇关于在 selenium 中,当我搜索 Xpath 时,如何捕获之前两个位置的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆