【问题标题】:Selecting an option from an optgroup with selenium and python使用 selenium 和 python 从 optgroup 中选择一个选项
【发布时间】:2018-05-07 15:55:02
【问题描述】:

我想从这个 optgroup 中选择一个值,然后它会给我一个链接下拉列表。

<div class="searchbar">
    <select id="q" multiple="" tabindex="-1" class="select2-hidden-accessible" aria-hidden="true">
    <option></option>
    <option class="q-all-text" value="al:all">Search all text</option>

    <optgroup label="Business Type">
        <option value="bt:Buyer">Buyer</option>
        <option value="bt:Farmer/Rancher">Farmer/Rancher</option>
        <option value="bt:Farmers Market">Farmers Market</option>
        <option value="bt:Fishery">Fishery</option>
        <option value="bt:Food Bank">Food Bank</option>
    </optgroup>

到目前为止,这是我的代码:

driver = webdriver.Chrome('/path/to/chromedriver')
driver.get("https://foodmarketmaker.com/main/mmsearch")
iframe = driver.find_element_by_tag_name("iframe")
driver.switch_to.frame(iframe)

select  = Select(driver.find_element_by_class_name("select2-hidden- accessible"))
select.select_by_value("bt:Farmer/Rancher")
links = driver.find_elements_by_tag_name('a')
print(links)
for link in links:
    print(link.get_attribute('href'))

我得到一个元素不存在的异常,或者当我通过索引访问时,由于 aria-hidden 属性为真,我得到一个 ElementNotVisibleException。有没有办法解决这个问题?

【问题讨论】:

  • 您可以模拟一些动作,例如悬停或单击某处使您的元素可见,然后选择该元素。如果它在一段时间内可见,则使用显式等待。或者您打算以任何方式选择该元素,然后使用 JavaScript 执行器将该 aria-hidden 更改为 false 并选择相同的

标签: python selenium web-crawler


【解决方案1】:

select 不是您需要处理的节点,因为它不可见。

尝试下面的代码以获得所需的输出:

from selenium import webdriver as web
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


driver = web.Chrome('/path/to/chromedriver')
driver.get("https://foodmarketmaker.com/main/mmsearch")
iframe = driver.find_element_by_tag_name("iframe")
driver.switch_to.frame(iframe)

wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//section[@id="search-right"]//input[@placeholder="start typing to search"]'))).click()
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//li[.="Bass"]'))).click()

links = wait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, '//section[@id="search-results"]//a[.//*[name()="svg"]]')))

for link in links:
    print(link.get_attribute('href'))

附:您需要将'//li[.="Bass"]' XPath 中li 节点的文本值替换为所需选项

【讨论】:

  • 我正在尝试滚动浏览结果,以便加载所有链接,但它不会滚动。是否有在 iframe 中滚动的特定方式?我尝试使用执行脚本并滚动到页面底部,但它什么也没做。
【解决方案2】:

根据您共享的 HTML,打印所有您必须诱导 WebDriverWait 切换到所需框架的选项,然后单击 搜索框 em>options 如下所示:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    options = Options()
    options.add_argument("start-maximized")
    options.add_argument("disable-infobars")
    options.add_argument("--disable-extensions")
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://foodmarketmaker.com/main/mmsearch")
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@src='https://search.foodmarketmaker.com']")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='searchbar']//input[@class='select2-search__field' and @placeholder='start typing to search']"))).click()
    links = driver.find_elements_by_xpath("//li[@class='select2-results__option' and @aria-label='Business Type']/strong//following::ul[1]//li")
    for link in links:
        print(link.get_attribute('innerHTML'))
    
  • 控制台输出:

    Buyer
    Buyer: Baked Goods
    Buyer: Beverages
    Buyer: Dairy
    Buyer: Fish &amp; Seafood
    Buyer: Fruit &amp; Nuts
    Buyer: Grains &amp; OilSeed
    Buyer: Herbs
    Buyer: Meat &amp; Poultry
    Buyer: Miscellaneous
    Buyer: Specialty Products
    Buyer: Sugar/Confectionary &amp; Snacks
    Buyer: Vegetables
    Eating &amp; Drinking Place
    Eating &amp; Drinking Place: Cafeteria &amp; Buffet
    Eating &amp; Drinking Place: Caterer
    Eating &amp; Drinking Place: Drinking Place
    Eating &amp; Drinking Place: Food Service Contractor
    Eating &amp; Drinking Place: Mobile Food Service
    Eating &amp; Drinking Place: Restaurant
    Eating &amp; Drinking Place: Snack &amp; Nonalcoholic Beverage Bar
    Farmer/Rancher
    Farmer/Rancher: Dairy
    Farmer/Rancher: Forage
    Farmer/Rancher: Fruits &amp; Nuts
    Farmer/Rancher: Grains
    Farmer/Rancher: Herbs
    Farmer/Rancher: Livestock Production (Beef Cattle)
    Farmer/Rancher: Meat &amp; Poultry
    Farmer/Rancher: Specialty Products
    Farmer/Rancher: Vegetables
    Farmers Market
    Fishery
    Fishery: Fish/Shellfish/Seafood
    Food Bank
    Food Bank: Food Bank
    Food Hub
    Food Retailer
    Food Retailer: Baked Goods Store
    Food Retailer: Beer, Wine &amp; Liquor Store
    Food Retailer: Candy, Nut &amp; Confectionery
    Food Retailer: Fish Market
    Food Retailer: Fruit &amp; Vegetable Market
    Food Retailer: Grocery/Convenience Store
    Food Retailer: Health Supplement Store
    Food Retailer: Meat &amp; Poultry Market
    Food Retailer: Other Direct Selling Establishments
    Food Retailer: Other Specialty Food Store
    General Member
    Processor/Packing Shed
    Processor/Packing Shed: Animal Food Manufacturing
    Processor/Packing Shed: Bakery Products
    Processor/Packing Shed: Beverages
    Processor/Packing Shed: Dairy Products
    Processor/Packing Shed: Fish/Shellfish/Seafood Products
    Processor/Packing Shed: Fruit &amp; Vegetable Products
    Processor/Packing Shed: Grain &amp; Oilseed Milling
    Processor/Packing Shed: Meat &amp; Poultry Products
    Processor/Packing Shed: Miscellaneous
    Processor/Packing Shed: Packing Shed
    Processor/Packing Shed: Sugar and Confectionery Products
    Tourism
    Tourism: Agritourism
    Tourism: Fishing Charter
    Tourism: Hunting
    Wholesaler
    Wholesaler: Beer, Wine &amp; Alcoholic Beverages
    Wholesaler: Confectionery &amp; Snacks
    Wholesaler: Dairy Products
    Wholesaler: Farm Products
    Wholesaler: Fish/Shellfish/Seafood
    Wholesaler: Fruit &amp; Vegetable
    Wholesaler: General Grocery
    Wholesaler: Meat &amp; Poultry
    Wholesaler: Other Grocery
    Wholesaler: Packaged Frozen Food
    Winery
    Winery: Blends or Generic Wine
    Winery: Fortified Wine
    Winery: Fruit Wine
    Winery: Other Wine Products
    Winery: Red Wine
    Winery: Sparkling Wine
    Winery: White Wine
    

【讨论】:

    猜你喜欢
    • 2019-03-27
    • 2010-12-01
    • 2021-03-28
    • 2014-04-26
    • 2021-12-27
    • 2015-07-13
    • 2020-09-17
    • 2021-04-04
    • 2023-03-28
    相关资源
    最近更新 更多