python - 无法让我的抓取工具完成循环以用完所有关键字

标签 python python-3.x selenium selenium-webdriver web-scraping

我用 python 结合 selenium 编写了一个脚本,以从网页中获取一些信息。要获取内容,需要进行一些点击并填写输入框才能产生结果。结果显示在新选项卡中。因此,有必要切换到该特定窗口来解析信息。我的脚本可以非常有效地完成这一切。

这是this one的后续帖子.

我面临的问题是我使用很少的关键字来获取信息。当使用单个关键字时,driver.close()可能会起作用,但我使用多个关键字,所以当我使用driver.close()时,没有还剩下更多窗口可以继续移动。

问题:如何让抓取工具关闭新选项卡(当它从那里抓取信息时)并切换回主窗口以循环执行该过程,直到没有更多关键字可供检查为止?

这是我到目前为止的脚本:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

link = "https://officialrecords.broward.org/AcclaimWeb/search/SearchTypeName"

def get_information(driver,url):
    for keyword in ['HMC DESIGN GROUP','HMC DESIGN GROUP']:
        driver.get(url)
        current = driver.current_window_handle
        wait.until(EC.element_to_be_clickable((By.ID, "btnButton"))).click()
        wait.until(EC.presence_of_element_located((By.ID,"SearchOnName"))).send_keys(keyword)
        wait.until(EC.presence_of_element_located((By.ID, "btnSearch"))).click()
        wait.until(EC.element_to_be_clickable((By.XPATH, "//td[contains(., 'HMC DESIGN GROUP')]"))).click()
        wait.until(EC.new_window_is_opened)
        driver.switch_to.window([window for window in driver.window_handles if window != current][0])
        for items in wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,".listDocDetails"))):
            print(items.text)
        # driver.switch_to.default_content()
        # driver.close()

if __name__ == "__main__":
    driver = webdriver.Chrome()
    wait = WebDriverWait(driver,10)
    try:
        get_information(driver,link)
    finally:
        driver.quit()

最佳答案

尝试实现以下解决方案:

def get_information(driver,url):
    driver.get(url)
    current = driver.current_window_handle
    wait.until(EC.element_to_be_clickable((By.ID, "btnButton"))).click()
    for keyword in ['HMC DESIGN GROUP','HMC DESIGN GROUP']:
        input_field = wait.until(EC.presence_of_element_located((By.ID,"SearchOnName")))
        input_field.clear()
        input_field.send_keys(keyword)
        wait.until(EC.presence_of_element_located((By.ID, "btnSearch"))).click()
        wait.until_not(EC.visibility_of_element_located((By.ID, "SearchingWaitImg")))
        wait.until(EC.element_to_be_clickable((By.XPATH, "//td[contains(., '%s')]" % keyword))).click()
        driver.switch_to.window([window for window in driver.window_handles if window != current][0])
        for items in wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,".listDocDetails"))):
            print(items.text)
        driver.close()
        driver.switch_to.window(current)

关于python - 无法让我的抓取工具完成循环以用完所有关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51347987/

相关文章:

python - 与 pyplot 中的三个子图中的两个共享 yaxis 标签

python - 如何使用 scipy.spatial.Delaunay 将所有点包含到无错误的三角剖分网格中?

python - 从 Django 1.3 移动到 Django 1.11 object_list

python - 在 Python 中使用 Selenium 单击图像抛出无效的 xpath

python:互斥位置参数

data-structures - Python中的排序链表

javascript - Paypal Checkout 客户端集成 - 返回订单 ID promise 的问题

python - Pyqt5 Qgraphicsview 在负载上适当缩放

xpath - Selenium,PHPUnit:当我尝试获取样式属性时,我得到的是空值而不是值

node.js - 通过我的应用程序在没有终端的情况下运行夜类?