Scrapy、Selenium、jQuery 下拉列表与具有样式="display: none;" 的 select 标签不一致的行为

Scrapy, Selenium, jQuery Dropdown inconsistent behavior with select tag having style="display: none;"

我正在尝试在 this page 上设置下拉菜单:

第一个下拉菜单和第四个下拉菜单非常相似(品牌和国家/地区)。这是我用来获取品牌 (oem) 和国家/地区的代码:

oem = Select(wd.find_element_by_css_selector("#alBrandsList"))
oem.select_by_visible_text("Acer")

countries = Select(wd.find_element_by_css_selector("#alCountriesList"))
countries.select_by_visible_text("Albania")

下拉菜单在技术上是隐藏的,但它似乎对 device/oem 下拉菜单有效。对于国家/地区下拉菜单,它表示内容不可见(它是)。这是它从中提取的 HTML 代码:

<select class="pretty-dropdown" datatosent="brand" id="alBrandsList" name="alBrandsList" selectorid="alPhoneModelsList" target="/AdvanceLookup/GetPhoneModels/" style="display: none;">
      ...
</select>
<button type="button" class="ui-multiselect ui-widget ui-state-default ui-corner-all" aria-haspopup="true" style="width: 232px;">
  <span class="ui-icon ui-icon-triangle-2-n-s"></span>
  <span>Please select brand(s)</span>
</button>

<select class="pretty-dropdown" datatosent="country" id="alCountriesList" name="alCountriesList" selectorid="alCarriersList" target="/AdvanceLookup/GetCarriers/" style="display: none;">
      ...
</select>
<button type="button" class="ui-multiselect ui-widget ui-state-default ui-corner-all" aria-haspopup="true" style="width: 232px;">
  <span class="ui-icon ui-icon-triangle-2-n-s"></span>
  <span>Please select country</span>
</button>

知道为什么它适用于第一个而不适用于第二个吗?

作为页面上第一个和第四个下拉菜单的 <select> 标记 https://willmyphonework.net/AdvanceLookup is having the property style="display: none; you can't use Select Class. Instead you need to to induce WebDriverWait for the element_to_be_clickable() and you can use the following :

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    driver.get('https://willmyphonework.net/AdvanceLookup')
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='ui-multiselect ui-widget ui-state-default ui-corner-all']//span[text()='Please select brand(s)']"))).click()
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='ui-multiselect-checkboxes ui-helper-reset']//li/label/input[@title='Acer']"))).click()
    driver.find_element_by_xpath("//ul[@class='ui-helper-reset']//li/a/span[@class='ui-icon ui-icon-circle-close']").click()
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='ui-multiselect ui-widget ui-state-default ui-corner-all']//span[text()='Please select country']"))).click()
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='ui-multiselect-checkboxes ui-helper-reset']//li/label/input[@title='Albania']"))).click()
    driver.find_element_by_xpath("//div[@class='ui-multiselect-menu ui-widget ui-widget-content ui-corner-all']//following::ul[6]//a[@class='ui-multiselect-close']/span[@class='ui-icon ui-icon-circle-close']").click()
    
  • 浏览器快照: