Selenium Webdriver切换到另一个窗口并解析数据

时间:2017-08-15 22:13:49

标签: python selenium-webdriver

https://officialrecords.broward.org/AcclaimWeb/search/SearchTypeName

Click on BANKATLANTIC

点击BANKATLANTIC

enter image description here

如何提取TransactionItemId? #在DocLink下

如何从第一份文件中提取文字?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
from time import sleep

driverurl = "https://officialrecords.broward.org/AcclaimWeb/search/SearchTypeName"
RecordDateInput = "8/15/2017"

driver = webdriver.Chrome()
driver.get(driverurl)
driver.find_element_by_id("btnButton").click()

driver.find_element_by_id("Direct").click() # Grantor

Name = "ba"
driver.find_element_by_id("SearchOnName").send_keys(Name)

DocType = "RELEASE/REVOKE/SATISFY OR TERMINATE (RST)"
driver.find_element_by_id("DocTypesDisplay-input").clear()
sleep(1)
driver.find_element_by_id("DocTypesDisplay-input").send_keys(DocType)

driver.find_element_by_id("RecordDateFrom").clear()
driver.find_element_by_id("RecordDateFrom").send_keys(RecordDateInput)
driver.find_element_by_id("RecordDateTo").clear()
driver.find_element_by_id("RecordDateTo").send_keys(RecordDateInput)

driver.execute_script("return arguments[0].scrollIntoView();", driver.find_element_by_id("btnSearch"))
driver.find_element_by_id("btnSearch").click()
sleep(7)

driver.find_elements_by_class_name("rowNumClass")[0].click()
sleep(3)

#######################################################
# solution:

driver.switch_to_window(driver.window_handles[1])
sleep(3)

html = driver.page_source 
soup = BeautifulSoup(html, "lxml") 

TransactionItemId = soup.findAll("div", { "class" : "listDocDetails" })[-1].find("a")['onclick'].split("'")[1]

2 个答案:

答案 0 :(得分:0)

我能看到获得包含$ 600,000的元素的唯一方法是获取包含“Consideration:”标签的元素,然后找到包含美元金额的以下DIV。通过包含文本查找元素的唯一方法是使用XPath。下面的一个工作。

//div[@class='detailLabel'][contains(.,'Consideration:')]/following-sibling::div

答案 1 :(得分:0)

试试这个:

driver.switch_to_window(driver.window_handles[1])
sleep(3)

html = driver.page_source 
soup = BeautifulSoup(html, "lxml") 

TransactionItemId = soup.findAll("div", { "class" : "listDocDetails" })[-1].find("a")['onclick'].split("'")[1]