gpt4 book ai didi

python - Scrapy、Selenium、jQuery Dropdown 与具有样式 ="display: none;"的选择标记不一致的行为

转载 作者:行者123 更新时间:2023-12-01 06:48:17 24 4
gpt4 key购买 nike

我正在尝试在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")

该下拉列表在技术上是隐藏的,但它似乎适用于设备/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拥有属性 style="display: none; 你不能使用Select 。相反,您需要为 element_to_be_clickable() 引发 WebDriverWait您可以使用以下 Locator Strategies :

  • 代码块:

    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()
  • 浏览器快照:

advance_lookup

关于python - Scrapy、Selenium、jQuery Dropdown 与具有样式 ="display: none;"的选择标记不一致的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59129095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com