如何在不同的选项卡/窗口中打开选择标记(下拉列表)的选项项?

2024-09-27 21:28:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用Python和Selenium来抓取这个网站,它要求您从下拉框中选择一个日期,然后单击search来查看计划应用程序。在

网址:https://services.wiltshire.gov.uk/PlanningGIS/LLPG/WeeklyList。在

我有代码来选择下拉框的第一个索引并按搜索。我该如何为下拉框中的所有日期选项打开多个窗口,还是逐个浏览这些窗口,以便我可以快速浏览?在

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options


options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome('/Users/weaabduljamac/Downloads/chromedriver', 
chrome_options=options)

url = 'https://services.wiltshire.gov.uk/PlanningGIS/LLPG/WeeklyList'
driver.get(url)

select = Select(driver.find_element_by_xpath('//*[@id="selWeek"]'))
select.select_by_index(1)

button = driver.find_element_by_id('csbtnSearch')
button.click()

app_numbers = driver.find_element_by_xpath('//*[@id="form1"]/table/tbody/tr[1]/td[1]/a').text
print(app_numbers)

下拉框HTML:

^{pr2}$

Tags: fromhttpsimportidbydriverseleniumservice
3条回答

根据您的问题,您将无法为不同的下拉选项打开多个窗口,因为<options>标记不包含任何href属性。它们将始终在同一个浏览器窗口中呈现新页面。在

但是,要从下拉列表中选择日期,然后click()搜索查看计划应用程序,可以使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.support.ui import Select
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.add_argument(' headless')
    options.add_argument("start-maximized")
    options.add_argument('disable-infobars')
    driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    url = 'https://services.wiltshire.gov.uk/PlanningGIS/LLPG/WeeklyList'
    driver.get(url)
    
    select = Select(driver.find_element_by_xpath("//select[@class='formitem' and @id='selWeek']"))
    list_options = select.options
    for item in range(len(list_options)):
        select = Select(driver.find_element_by_xpath("//select[@class='formitem' and @id='selWeek']"))
        select.select_by_index(str(item))
        driver.find_element_by_css_selector("input.formbutton#csbtnSearch").click()
        print(driver.find_element_by_xpath('//*[@id="form1"]/table/tbody/tr[1]/td[1]/a').text)
        driver.get(url)
    driver.quit()
    
  • 控制台输出:

    18/06760/FUL
    18/07187/LBC
    18/06843/FUL
    18/06705/FUL
    18/06449/FUL
    18/05534/FUL
    18/06030/DEM
    18/05784/FUL
    18/05914/LBC
    18/05241/FUL
    

琐事

要清除所有需要替换的链接:

find_element_by_xpath('//*[@id="form1"]/table/tbody/tr[1]/td[1]/a')

有:

find_elements_by_xpath('//*[@id="form1"]/table/tbody/tr[1]/td[1]/a')

您可以在“搜索”按钮上执行click + ctrl,在新窗口中打开链接,丢弃数据,并返回到第一页以选择下一个选项

# original window to switch back
window_before = driver.window_handles[0]

select = Select(driver.find_element_by_id('selWeek'))
options = select.options
for option in options :
    select.select_by_visible_text(option.text)

    # click to open link in new window
    button = driver.find_element_by_id('csbtnSearch')
    ActionChains(driver).key_down(Keys.CONTROL).click(button).key_up(Keys.CONTROL).perform()

    # switch to new window and scrap the data
    driver.switch_to_window(driver.window_handles[1])

    # scrap the data

    # return to original window
    driver.close()
    driver.switch_to_window(window_before)

我很确定这是不可能的,你必须循环使用这些选项并将数据存储在某处,然后从每个下拉列表中添加新数据。在

希望这有帮助。在

相关问题 更多 >

    热门问题