我在访问案例链接列表的网站时遇到问题,需要单击所有链接。
该链接提交一个表单,并重新加载同一页面:
- 每个案例的新链接,以及
- 符合
window.open()
我需要的案例数据。
在普通的网络浏览器中,我会弹出一个包含案例数据的窗口。
从 Selenium 运行时,我可以单击链接,它会加载页面,并将我重定向到案例页面,但是我无法返回结果页面,并且我无法保留结果第一页的链接列表因为它们在每次点击后都是陈旧的。
任何想法如何同时获得?
import undetected_chromedriver.v2 as uc
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import sys
'''
Requires chrome
pip install selenium
pip install undetected_chromedriver
'''
driver = uc.Chrome()
txtUsingTestDates = 'Using test dates'
print(txtUsingTestDates.center(20, "="))
dateStart = "08/02/2021"
dateEnd = "08/02/2021"
with driver:
driver.get('https://publicindex.sccourts.org/abbeville/publicindex/')
try:
formAcceptTerms = driver.find_element(By.XPATH, "//*[@id='form1']")
except:
print("Didn't find form")
try:
driver.find_element(By.XPATH, "//*[@id='ContentPlaceHolder1_ButtonAccept']").click()
print(">>>Accepting terms")
except:
print("Didn't find button")
### Search form
#Select court
selectorCourts = Select(driver.find_element(By.XPATH, "//*[@id='ContentPlaceHolder1_DropDownListCourtType']"))
selectorCourts.select_by_visible_text('Circuit Court')
#Select case type
selectorCaseType = Select(driver.find_element(By.XPATH, "//*[@id='ContentPlaceHolder1_DropDownListCaseTypes']"))
selectorCaseType.select_by_visible_text('Criminal-Clerk')
#Select date search for filing date
selectorDateSearchType = Select(driver.find_element(By.XPATH, "//*[@id='ContentPlaceHolder1_DropDownListDateFilter']"))
selectorDateSearchType.select_by_visible_text('Case Filed')
#Fill from and to date
textBoxDateFrom = driver.find_element(By.XPATH, "//*[@id='ContentPlaceHolder1_TextBoxDateFrom']")
textBoxDateFrom.send_keys(dateStart)
textBoxDateTo = driver.find_element(By.XPATH, "//*[@id='ContentPlaceHolder1_TextBoxDateTo']")
textBoxDateTo.send_keys(dateEnd)
get_cookies = driver.get_cookies()
print(get_cookies)
#Submit search
driver.find_element(By.XPATH, "//*[@id='ContentPlaceHolder1_ButtonSearch']").click()
#Results
tableResults = driver.find_element(By.XPATH, "//*[@id='ContentPlaceHolder1_SearchResults']")
tableBody = tableResults.find_element(By.TAG_NAME, "tbody")
tableRows = tableBody.find_elements(By.TAG_NAME, "tr") #Gets the table rows
caseLinks = []
for tableRow in tableRows:
tableCols = tableRow.find_elements(By.TAG_NAME, "td")
if len(tableCols) > 1:
columnCase = tableCols[2]
caseNumber = columnCase.text
print('Found case number ' + caseNumber)
caseLinks.append(columnCase)
else:
print("--Skipping header")
for column in caseLinks:
caseNumber = column.text
print('Clicking ' + caseNumber)
column.find_element(By.PARTIAL_LINK_TEXT, caseNumber).click()
#Save case data
#Return to updated case page to find next link
driver.close()