如何在不同的选项卡/窗口中打开选择标签(下拉菜单)的选项? [英] How to open the option items of a select tag (dropdown) in different tabs/windows?

查看:20
本文介绍了如何在不同的选项卡/窗口中打开选择标签(下拉菜单)的选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python 和 Selenium 抓取此网站,它需要您从下拉框中选择一个日期,然后单击搜索以查看规划应用程序.

I'm trying to scrape this website using Python and Selenium, it requires you to select a date from drop-down box then click search to view the planning applications.

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

我有代码可以选择下拉框的第一个索引并按搜索.我如何为下拉框中的所有日期选项打开多个窗口,或者一个一个地浏览它们以便我可以抓取它?

I have the code working to select the first index of the drop-down box and press search. How would I open multiple windows for all the date options in the drop-down box or go through them one by one so I can scrape it?

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:

<select class="formitem" id="selWeek" name="selWeek">
   <option selected="selected" value="2018,31">Week commencing Monday 30 July 2018</option>
   <option value="2018,30">Week commencing Monday 23 July 2018</option>
   <option value="2018,29">Week commencing Monday 16 July 2018</option>
   <option value="2018,28">Week commencing Monday 9 July 2018</option>
   <option value="2018,27">Week commencing Monday 2 July 2018</option>
   <option value="2018,26">Week commencing Monday 25 June 2018</option>
   <option value="2018,25">Week commencing Monday 18 June 2018</option>
   <option value="2018,24">Week commencing Monday 11 June 2018</option>
   <option value="2018,23">Week commencing Monday 4 June 2018</option>
   <option value="2018,22">Week commencing Monday 28 May 2018</option>
</select>

推荐答案

根据您的问题,您将无法像 <那样为不同的下拉选项打开多个窗口/code> 标签不包含任何 href 属性.他们将始终在同一个浏览器窗口中呈现新页面.

As per your question you won't be able to open multiple windows for different drop-down options as the <options> tags doesn't contains any href attribute. They will always render the new page in the same browser window.

但是,从下拉菜单中选择一个日期,然后click() 搜索以查看规划应用程序,您可以使用以下解决方案:

However to select a date from the Dropdown and then click() Search to view the planning applications you can use the following solution:

  • 代码块:

  • Code Block:

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:UtilityBrowserDriverschromedriver.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()

  • 控制台输出:

  • Console Output:

    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
    

  • 要抓取您需要替换的所有链接:

    To scrape all the links you need to replace:

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

    这篇关于如何在不同的选项卡/窗口中打开选择标签(下拉菜单)的选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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